java時間戳轉(zhuǎn)換為日期格式的多種方式
java時間戳轉(zhuǎn)換為日期格式
以下是幾種將Java時間戳轉(zhuǎn)換為日期格式的實現(xiàn)方法:
方法一:使用Java的Date類和SimpleDateFormat類
long timestamp = 1617142526000L; // 時間戳值 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; // 時間戳值 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; // 時間戳值 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類,使用自定義時區(qū)
long timestamp = 1617142526000L; // 時間戳值 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; // 時間戳值 Date date = new Date(timestamp); String formattedDate = DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss"); System.out.println(formattedDate);
以上是幾種將Java時間戳轉(zhuǎn)換為日期格式的實現(xiàn)方法,它們都是可行的,并且可以根據(jù)具體需求選擇適合的方法。
到此這篇關(guān)于java時間戳轉(zhuǎn)換為日期格式的文章就介紹到這了,更多相關(guān)java時間戳轉(zhuǎn)換為日期內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringMVC整合SpringSession 實現(xiàn)sessiong
這篇文章主要介紹了SpringMVC整合SpringSession 實現(xiàn)session的實例代碼,本文通過實例相結(jié)合的形式給大家介紹的非常詳細(xì),需要的朋友參考下吧2018-04-04Java畢業(yè)設(shè)計實戰(zhàn)之財務(wù)預(yù)算管理系統(tǒng)的實現(xiàn)
這是一個使用了java+SSM+Jsp+Mysql+Layui+Maven開發(fā)的財務(wù)預(yù)算管理系統(tǒng),是一個畢業(yè)設(shè)計的實戰(zhàn)練習(xí),具有財務(wù)預(yù)算管理該有的所有功能,感興趣的朋友快來看看吧2022-02-02java使用xstream實現(xiàn)xml文件和對象之間的相互轉(zhuǎn)換
xml是一個用途比較廣泛的文件類型,在java里也自帶解析xml的包,但是本文使用的是xstream來實現(xiàn)xml和對象之間的相互轉(zhuǎn)換,xstream是一個第三方開源框架,使用起來比較方便,對java?xml和對象轉(zhuǎn)換相關(guān)知識感興趣的朋友一起看看吧2023-09-09關(guān)于HttpClient 引發(fā)的線程太多導(dǎo)致FullGc的問題
這篇文章主要介紹了關(guān)于HttpClient 引發(fā)的線程太多導(dǎo)致FullGc的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01基于Mybatis實現(xiàn)動態(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 框架實現(xiàn)動態(tài)切換數(shù)據(jù)源功能,需要的朋友可以參考下2024-09-09