Java中字符串轉(zhuǎn)時(shí)間與時(shí)間轉(zhuǎn)字符串的操作詳解
一、字符串轉(zhuǎn)時(shí)間
在 Java 中,可以使用 java.time
包中的 DateTimeFormatter
類將字符串格式的日期時(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
類。
(一)使用預(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
類。
(一)字符串轉(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í)間處理功能,通過 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)文章
使用EasyExcel實(shí)現(xiàn)簡(jiǎn)單的Excel表格解析操作
這篇文章主要介紹了如何使用EasyExcel完成簡(jiǎn)單的表格解析操作,同時(shí)實(shí)現(xiàn)了大量數(shù)據(jù)情況下數(shù)據(jù)的分次批量入庫,并記錄每條數(shù)據(jù)入庫的狀態(tài),感興趣的可以了解下2025-03-03深入理解SpringBoot中關(guān)于Mybatis使用方法
這篇文章主要介紹了SpringBoot中關(guān)于Mybatis使用方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-03-03Java數(shù)組的遍歷與求和知識(shí)點(diǎn)
本篇文章給大家總計(jì)了Java數(shù)組的遍歷與求和的知識(shí)點(diǎn)以及需要注意的地方,需要的朋友參考學(xué)習(xí)下。2018-02-02JDK20?+?SpringBoot?3.1.0?+?JdbcTemplate?使用案例詳解
通過 JdbcTemplate 直接執(zhí)行 SQL 語句,結(jié)合源碼動(dòng)態(tài)編譯即可方便實(shí)現(xiàn)動(dòng)態(tài)修改代碼邏輯的效果,這篇文章主要介紹了JDK20?+?SpringBoot?3.1.0?+?JdbcTemplate?使用,需要的朋友可以參考下2023-09-09Java 中組合模型之對(duì)象結(jié)構(gòu)模式的詳解
這篇文章主要介紹了Java 中組合模型之對(duì)象結(jié)構(gòu)模式的詳解的相關(guān)資料,希望通過本文能幫助到大家理解應(yīng)用對(duì)象結(jié)構(gòu)模型,需要的朋友可以參考下2017-09-09