java時(shí)間戳轉(zhuǎn)換為日期格式的多種方式
java時(shí)間戳轉(zhuǎn)換為日期格式
以下是幾種將Java時(shí)間戳轉(zhuǎn)換為日期格式的實(shí)現(xiàn)方法:
方法一:使用Java的Date類和SimpleDateFormat類
long timestamp = 1617142526000L; // 時(shí)間戳值 Date date = new Date(timestamp); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String formattedDate = sdf.format(date); System.out.println(formattedDate);
方法二:使用Java 8的LocalDateTime類和DateTimeFormatter類
long timestamp = 1617142526000L; // 時(shí)間戳值 Instant instant = Instant.ofEpochMilli(timestamp); LocalDateTime date = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String formattedDate = date.format(formatter); System.out.println(formattedDate);
方法三:使用Java 8的java.time包中的Instant類和DateTimeFormatter類
long timestamp = 1617142526000L; // 時(shí)間戳值 Instant instant = Instant.ofEpochMilli(timestamp); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault()); String formattedDate = zonedDateTime.format(formatter); System.out.println(formattedDate);
方法四:使用Java 8的java.time包中的Instant類和DateTimeFormatter類,使用自定義時(shí)區(qū)
long timestamp = 1617142526000L; // 時(shí)間戳值 Instant instant = Instant.ofEpochMilli(timestamp); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, ZoneId.of("Asia/Shanghai")); String formattedDate = zonedDateTime.format(formatter); System.out.println(formattedDate);
方法五:使用Apache Commons Lang庫的DateUtils類
import org.apache.commons.lang3.time.DateFormatUtils; import java.util.Date; long timestamp = 1617142526000L; // 時(shí)間戳值 Date date = new Date(timestamp); String formattedDate = DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss"); System.out.println(formattedDate);
以上是幾種將Java時(shí)間戳轉(zhuǎn)換為日期格式的實(shí)現(xiàn)方法,它們都是可行的,并且可以根據(jù)具體需求選擇適合的方法。
到此這篇關(guān)于java時(shí)間戳轉(zhuǎn)換為日期格式的文章就介紹到這了,更多相關(guān)java時(shí)間戳轉(zhuǎn)換為日期內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringMVC整合SpringSession 實(shí)現(xiàn)sessiong
這篇文章主要介紹了SpringMVC整合SpringSession 實(shí)現(xiàn)session的實(shí)例代碼,本文通過實(shí)例相結(jié)合的形式給大家介紹的非常詳細(xì),需要的朋友參考下吧2018-04-04Java畢業(yè)設(shè)計(jì)實(shí)戰(zhàn)之財(cái)務(wù)預(yù)算管理系統(tǒng)的實(shí)現(xiàn)
這是一個(gè)使用了java+SSM+Jsp+Mysql+Layui+Maven開發(fā)的財(cái)務(wù)預(yù)算管理系統(tǒng),是一個(gè)畢業(yè)設(shè)計(jì)的實(shí)戰(zhàn)練習(xí),具有財(cái)務(wù)預(yù)算管理該有的所有功能,感興趣的朋友快來看看吧2022-02-02java使用xstream實(shí)現(xiàn)xml文件和對(duì)象之間的相互轉(zhuǎn)換
xml是一個(gè)用途比較廣泛的文件類型,在java里也自帶解析xml的包,但是本文使用的是xstream來實(shí)現(xiàn)xml和對(duì)象之間的相互轉(zhuǎn)換,xstream是一個(gè)第三方開源框架,使用起來比較方便,對(duì)java?xml和對(duì)象轉(zhuǎn)換相關(guān)知識(shí)感興趣的朋友一起看看吧2023-09-09關(guān)于HttpClient 引發(fā)的線程太多導(dǎo)致FullGc的問題
這篇文章主要介紹了關(guān)于HttpClient 引發(fā)的線程太多導(dǎo)致FullGc的問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01基于Mybatis實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源切換的示例代碼
在當(dāng)今的互聯(lián)網(wǎng)應(yīng)用中,微服務(wù)大行其道,隨著業(yè)務(wù)的發(fā)展和擴(kuò)展,單一的數(shù)據(jù)庫無法滿足日益增長的數(shù)據(jù)需求,本文將基于 JDK17 + Spring Boot 3 和 MyBatis 框架實(shí)現(xiàn)動(dòng)態(tài)切換數(shù)據(jù)源功能,需要的朋友可以參考下2024-09-09