開始使用免費開始

時區轉換

你正在跨不同行政時區追蹤電子產品庫存的生產日期。把生產日期在兩個時區之間互相轉換!

所需的套件如 LocalDateZoneIdZonedDateTime 都已為你匯入。

本練習屬於課程

使用 Java 進行資料清理

檢視課程

練習說明

  • date 轉換為紐約時區的當日開始時間。
  • 將紐約時間 nyTime 轉換為洛杉磯時間 laTime

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

public class DateStandardizationExample {
    public static void main(String[] args) {
        LocalDate date = LocalDate.parse("2023-01-01");
        
        // Convert the date to the New York time zone at the start of day
        ZonedDateTime nyTime = ____.____(ZoneId.of("America/New_York"));
        // Convert the New York time to Los Angeles time
        ZonedDateTime laTime = ____.____(ZoneId.of("America/Los_Angeles"));
        
        System.out.println("New York time: " + nyTime);
		System.out.println("Los Angeles time: " + laTime);
    }
}
編輯並執行程式碼