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)文章
SpringBoot集成Kafka并使用多個(gè)死信隊(duì)列詳解
這篇文章主要為大家詳細(xì)介紹了SpringBoot集成Kafka并使用多個(gè)死信隊(duì)列的示例代碼,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-05-05
springboot過濾器執(zhí)行兩次的解決及跨域過濾器問題
這篇文章主要介紹了springboot過濾器執(zhí)行兩次的解決及跨域過濾器問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
Java多線程編程之CountDownLatch同步工具使用實(shí)例
這篇文章主要介紹了Java多線程編程之CountDownLatch同步工具使用實(shí)例,需要的朋友可以參考下2015-05-05
Java驗(yàn)證日期時(shí)間字符串是否合法的三種方式
判斷日期經(jīng)常合法出現(xiàn)在IO場(chǎng)景下,下面將盡量使用簡(jiǎn)練的思路和代碼呈現(xiàn)給大伙,這篇文章主要給大家介紹了關(guān)于Java驗(yàn)證日期時(shí)間字符串是否合法的三種方式,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11
Java多態(tài)向上轉(zhuǎn)型和向下轉(zhuǎn)型及優(yōu)點(diǎn)總結(jié)
多態(tài)指不同對(duì)象對(duì)同一方法呈現(xiàn)不同行為,通過向上轉(zhuǎn)型和重寫實(shí)現(xiàn),提升代碼靈活性與可擴(kuò)展性,降低復(fù)雜度,這篇文章主要介紹了Java多態(tài)向上轉(zhuǎn)型和向下轉(zhuǎn)型及優(yōu)點(diǎn)的相關(guān)資料,需要的朋友可以參考下2025-06-06
EditPlus運(yùn)行java時(shí)從鍵盤輸入數(shù)據(jù)的操作方法
這篇文章主要介紹了EditPlus運(yùn)行java時(shí)從鍵盤輸入數(shù)據(jù)的操作方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
使用MyBatis 動(dòng)態(tài)update數(shù)據(jù)
使用mybatis寫sql,需要?jiǎng)討B(tài)更新對(duì)象數(shù)據(jù),每次需要更新的字段不同,為了防止null空異常,就需要用動(dòng)態(tài)sql了,下面給大家分享一段代碼關(guān)于mybatis動(dòng)態(tài)update,需要的朋友參考下2016-11-11
Java中的自動(dòng)裝箱與自動(dòng)拆箱的實(shí)現(xiàn)
自動(dòng)裝箱和自動(dòng)拆箱使得我們?cè)谑褂没緮?shù)據(jù)類型時(shí)更加方便,同時(shí)也提高了代碼的可讀性和健壯性,本文將詳細(xì)介紹Java中的自動(dòng)裝箱和自動(dòng)拆箱機(jī)制,感興趣的可以了解一下2023-08-08

