GMT(Greenwich Mean Time): GMT 是格林威治標準時間,基於地球自轉,定義為通過倫敦格林威治天文台的子午線(本初子午線)的時間。 UTC(Coordinated Universal Time):UTC 是全球協調時間,基於原子鐘的精確計時,與 GMT 基本一致。 在 Java 等程式語言中,通常使用 UTC 作為標準時間,如 Instant.now() 獲取的是 UTC 時間。
台灣的時區是 GMT+8,這表示台灣的時間比格林威治標準時間(GMT)快 8 小時 而英國的時區為 GMT+0 ,這意味著英國的時間與 GMT 相同。 而這兩個地區的時間差了 8 小時 。
如果在台灣的時間是 2025年2月19日 15:30(GMT+8),那麼同一個時間裡,在英國(GMT+0)看到的時間會是 2025年2月19日 15:30 - 8小時 = 2025年2月19日 07:30
Timestamp(時間戳) 是一個整數,代表著從 UTC 1970 年 1 月 1 日 0 時 0 分 0 秒 起至現在的總秒數。
在 Java 中使用 System.currentTimeMillis() 取得 Timestamp
Java 8 以前, 使用 Date ,而 Date 有以下缺陷,不建議使用
是可變的(mutable),是執行緒不安全的
時區 和 Timestamp 混在一起,會因不同的電腦系統導致顯示不同時間。
若要處理時區需借助 SimpleDateFormat 或其他 Library 輔助。
SimpleDateFormat 是執行緒不安全的
日期與時間沒有分開,無法只單獨處理時間或是日期。
你只想要日期的話,在 Date 物件中仍然會有時間的部分,它會顯示為 2025-02-06 00:00:00
Java 8 推出了 java.time
。
無時區的日期時間 LocalDate
LocalDate 代表日期,只儲存了年、月、日。
是不可變的(immutable)
1 2 3 4 5 6 7 8 9 10 LocalDate date = LocalDate.now();System.out.println(date1); date = LocalDate.of(2025 , 2 , 6 ); date = date.plusDays(5 ); System.out.println(date.getYear()); System.out.println(date.getMonth()); System.out.println(date.getMonthValue()); System.out.println(date.getDayOfMonth());
LocalTime
代表時間,只儲存時間,時、分、秒,此外,也儲存奈秒。
是不可變的(immutable)
1 2 3 4 5 6 7 LocalTime time = LocalTime.now();System.out.println(time); time = time.minusSeconds(5 ); System.out.println(time.getHour()); System.out.println(time.getMinute()); System.out.println(time.getSecond());
LocalDateTime
封裝了 LocalDate
和 LocalTime
代表日期時間
是不可變的(immutable)
1 2 3 4 5 6 7 8 9 10 11 LocalDateTime dateTime = LocalDateTime.now();System.out.println(dateTime); dateTime = dateTime.plusDays(30 ); System.out.println(dateTime.getYear()); System.out.println(dateTime.getMonthValue()); System.out.println(dateTime.getDayOfMonth()); System.out.println(dateTime.getHour()); System.out.println(dateTime.getMinute()); System.out.println(dateTime.getSecond());
DateTimeFormatter
和 SimpleDateFormat 類似,用來進行日期時間物件,與字串之間的互換,但不用處理 ParseException 這個 checked exception。
1 2 3 4 5 var formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss" );LocalDateTime dateTime = LocalDateTime.parse("2025/02/06 01:02:03" , formatter);System.out.println(dateTime); System.out.println(formatter.format(dateTime));
有時區的日期時間 ZonedDateTime
ZoneId
可以使用 ZoneId.getAvailableZoneIds()
取得支援的 key 值。
1 2 3 4 5 6 7 8 ZonedDateTime now = ZonedDateTime.now();System.out.println("Current ZonedDateTime: " + now); ZonedDateTime nowInUTC = ZonedDateTime.now(ZoneId.of("UTC" ));System.out.println("Current time in UTC: " + nowInUTC); ZonedDateTime nowInTaipei = ZonedDateTime.now(ZoneId.of("Asia/Taipei" ));System.out.println("Current time in Taipei: " + nowInTaipei);
亦可直接使用偏移量即 UTC/GMT,來定義 ZoneId
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 var dateTime = LocalDateTime.now();var tokyoZoneId = ZoneId.of("+0900" );var tokyoDateTime = ZonedDateTime.of(dateTime, tokyoZoneId);var londonZoneId = ZoneId.of("+0000" );var londonDateTime = tokyoDateTime.withZoneSameInstant(londonZoneId);System.out.println(tokyoDateTime); System.out.println(londonDateTime);
參考