fork download
  1. import java.time.LocalDate;
  2. import java.time.LocalTime;
  3. import java.time.Month;
  4. import java.time.ZoneId;
  5. import java.time.ZonedDateTime;
  6.  
  7. class Main {
  8. public static void main(String[] args) {
  9. // Using the default ZoneId set to your JVM i.e. it is the same as
  10. // ZonedDateTime.now(ZoneId.systemDefault())
  11. ZonedDateTime nowInDefaultZone = ZonedDateTime.now();
  12. System.out.println(nowInDefaultZone);
  13.  
  14. // Using a specified ZoneId. Note: India does not observe DST
  15. ZonedDateTime nowInIndia = ZonedDateTime.now(ZoneId.of("Asia/Kolkata"));
  16. System.out.println(nowInIndia);
  17.  
  18. // ZoneId.of("America/New_York") observes DST
  19. ZoneId zoneId = ZoneId.of("America/New_York");
  20.  
  21. // A date-time in ZoneId.of("America/New_York") during DST
  22. ZonedDateTime zdtNyDuringDST = ZonedDateTime.of(
  23. LocalDate.of(2024, Month.DECEMBER, 20),
  24. LocalTime.MIN,
  25. zoneId);
  26. System.out.println(zdtNyDuringDST);
  27.  
  28. // A date-time in ZoneId.of("America/New_York") outside DST period
  29. ZonedDateTime zdtNyOutsideDST = ZonedDateTime.of(
  30. LocalDate.of(2025, Month.MAY, 20),
  31. LocalTime.MIN,
  32. zoneId);
  33. System.out.println(zdtNyOutsideDST);
  34. }
  35. }
Success #stdin #stdout 0.13s 62468KB
stdin
Standard input is empty
stdout
2024-12-20T13:34:15.009891Z[GMT]
2024-12-20T19:04:15.062966+05:30[Asia/Kolkata]
2024-12-20T00:00-05:00[America/New_York]
2025-05-20T00:00-04:00[America/New_York]