Java中字符串轉時間與時間轉字符串的操作詳解
一、字符串轉時間
在 Java 中,可以使用 java.time 包中的 DateTimeFormatter 類將字符串格式的日期時間轉換為 LocalDateTime 或 ZonedDateTime 對象。
(一)使用預定義格式
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);
}
}
二、時間轉字符串
將日期時間對象格式化為字符串,可以使用 DateTimeFormatter 類。
(一)使用預定義格式
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 類。
(一)字符串轉帶時區(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ū)的日期時間轉字符串
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);
}
}
四、總結
Java 的 java.time 包提供了強大的日期和時間處理功能,通過 DateTimeFormatter 可以輕松地在日期時間對象和字符串之間進行轉換。
到此這篇關于Java中字符串轉時間與時間轉字符串的操作詳解的文章就介紹到這了,更多相關Java字符串與時間互轉內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
JDK20?+?SpringBoot?3.1.0?+?JdbcTemplate?使用案例詳解
通過 JdbcTemplate 直接執(zhí)行 SQL 語句,結合源碼動態(tài)編譯即可方便實現動態(tài)修改代碼邏輯的效果,這篇文章主要介紹了JDK20?+?SpringBoot?3.1.0?+?JdbcTemplate?使用,需要的朋友可以參考下2023-09-09

