Java中字符串轉(zhuǎn)時間與時間轉(zhuǎn)字符串的操作詳解
一、字符串轉(zhuǎn)時間
在 Java 中,可以使用 java.time
包中的 DateTimeFormatter
類將字符串格式的日期時間轉(zhuǎn)換為 LocalDateTime
或 ZonedDateTime
對象。
(一)使用預(yù)定義格式
import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class StringToDateExample { public static void main(String[] args) { String isoDateTime = "2023-10-11T12:34:56"; DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME; LocalDateTime dateTime = LocalDateTime.parse(isoDateTime, formatter); System.out.println("解析后的日期時間: " + dateTime); } }
(二)自定義格式
import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class CustomStringToDateExample { public static void main(String[] args) { String customDateTime = "2023-10-11 12:34:56"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDateTime dateTime = LocalDateTime.parse(customDateTime, formatter); System.out.println("解析后的日期時間: " + dateTime); } }
二、時間轉(zhuǎn)字符串
將日期時間對象格式化為字符串,可以使用 DateTimeFormatter
類。
(一)使用預(yù)定義格式
import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class DateToStringExample { public static void main(String[] args) { LocalDateTime now = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME; String formattedDate = now.format(formatter); System.out.println("ISO格式日期時間: " + formattedDate); } }
(二)自定義格式
import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class CustomDateToStringExample { public static void main(String[] args) { LocalDateTime now = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String formattedDate = now.format(formatter); System.out.println("自定義格式日期時間: " + formattedDate); } }
三、處理不同時區(qū)的日期
在處理不同時區(qū)的日期時,可以使用 ZonedDateTime
類。
(一)字符串轉(zhuǎn)帶時區(qū)的日期時間
import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; public class StringToZonedDateTimeExample { public static void main(String[] args) { String zonedDateTimeString = "2023-10-11T12:34:56-04:00"; DateTimeFormatter formatter = DateTimeFormatter.ISO_ZONED_DATE_TIME; ZonedDateTime zonedDateTime = ZonedDateTime.parse(zonedDateTimeString, formatter); System.out.println("解析后的帶時區(qū)日期時間: " + zonedDateTime); } }
(二)帶時區(qū)的日期時間轉(zhuǎn)字符串
import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; public class ZonedDateTimeToStringExample { public static void main(String[] args) { ZonedDateTime now = ZonedDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z"); String formattedDate = now.format(formatter); System.out.println("帶時區(qū)的日期時間字符串: " + formattedDate); } }
四、總結(jié)
Java 的 java.time
包提供了強大的日期和時間處理功能,通過 DateTimeFormatter
可以輕松地在日期時間對象和字符串之間進行轉(zhuǎn)換。
到此這篇關(guān)于Java中字符串轉(zhuǎn)時間與時間轉(zhuǎn)字符串的操作詳解的文章就介紹到這了,更多相關(guān)Java字符串與時間互轉(zhuǎn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java數(shù)據(jù)結(jié)構(gòu)之HashMap源碼深入分析
Java HashMap是一種基于哈希表實現(xiàn)的鍵值對存儲結(jié)構(gòu),可以實現(xiàn)快速的數(shù)據(jù)查找和存儲。它是線程不安全的,但在單線程環(huán)境中運行效率高,被廣泛應(yīng)用于Java開發(fā)中2023-04-04springboot中redis的緩存穿透問題實現(xiàn)
這篇文章主要介紹了springboot中redis的緩存穿透問題實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02Eclipse中創(chuàng)建Web項目最新方法(2023年)
在Java開發(fā)人員中,最常用的開發(fā)工具應(yīng)該就是Eclipse,下面這篇文章主要給大家介紹了關(guān)于Eclipse中創(chuàng)建Web項目2023年最新的方法,需要的朋友可以參考下2023-09-09詳解Spring 參數(shù)驗證@Validated和@Valid的區(qū)別
這篇文章主要介紹了詳解參數(shù)驗證 @Validated 和 @Valid 的區(qū)別,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01spring mvc中直接注入的HttpServletRequst安全嗎
這篇文章主要給大家介紹了關(guān)于spring mvc中直接注入的HttpServletRequst是不是安全的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2018-04-04實例詳解Spring Boot實戰(zhàn)之Redis緩存登錄驗證碼
本章簡單介紹redis的配置及使用方法,本文示例代碼在前面代碼的基礎(chǔ)上進行修改添加,實現(xiàn)了使用redis進行緩存驗證碼,以及校驗驗證碼的過程。感興趣的的朋友一起看看吧2017-08-08SpringBoot使用自定義注解實現(xiàn)數(shù)據(jù)脫敏過程詳細(xì)解析
這篇文章主要介紹了SpringBoot自定義注解之脫敏注解詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02