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 包提供了強(qiáng)大的日期和時間處理功能,通過 DateTimeFormatter 可以輕松地在日期時間對象和字符串之間進(jìn)行轉(zhuǎn)換。
到此這篇關(guān)于Java中字符串轉(zhuǎn)時間與時間轉(zhuǎn)字符串的操作詳解的文章就介紹到這了,更多相關(guān)Java字符串與時間互轉(zhuǎn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot集成Kafka并使用多個死信隊(duì)列詳解
這篇文章主要為大家詳細(xì)介紹了SpringBoot集成Kafka并使用多個死信隊(duì)列的示例代碼,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-05-05
springboot過濾器執(zhí)行兩次的解決及跨域過濾器問題
這篇文章主要介紹了springboot過濾器執(zhí)行兩次的解決及跨域過濾器問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
Java多線程編程之CountDownLatch同步工具使用實(shí)例
這篇文章主要介紹了Java多線程編程之CountDownLatch同步工具使用實(shí)例,需要的朋友可以參考下2015-05-05
Java多態(tài)向上轉(zhuǎn)型和向下轉(zhuǎn)型及優(yōu)點(diǎn)總結(jié)
多態(tài)指不同對象對同一方法呈現(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ù)據(jù)的操作方法
這篇文章主要介紹了EditPlus運(yùn)行java時從鍵盤輸入數(shù)據(jù)的操作方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
使用MyBatis 動態(tài)update數(shù)據(jù)
使用mybatis寫sql,需要動態(tài)更新對象數(shù)據(jù),每次需要更新的字段不同,為了防止null空異常,就需要用動態(tài)sql了,下面給大家分享一段代碼關(guān)于mybatis動態(tài)update,需要的朋友參考下2016-11-11

