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