fork(2) download
  1. import java.time.Instant;
  2. import java.time.ZoneId;
  3. import java.time.ZonedDateTime;
  4.  
  5. public class Main {
  6. public static void main(String[] args) {
  7. Instant now = Instant.now();
  8.  
  9. ZonedDateTime zdtUsEastern = now.atZone(ZoneId.of("America/New_York"));
  10. System.out.println(zdtUsEastern);
  11.  
  12. ZonedDateTime zdtUsCentral = now.atZone(ZoneId.of("America/Mexico_City"));
  13. System.out.println(zdtUsCentral);
  14.  
  15. // If you have to deal with just one time zone, you can use ZonedDateTime
  16. // directly (i.e. without using Instant)
  17. ZonedDateTime zdtLondon = ZonedDateTime.now(ZoneId.of("Asia/Kolkata"));
  18. System.out.println(zdtLondon);
  19.  
  20. // ZonedDateTime in JVM's time zone
  21. ZonedDateTime zdtJvmDefault = ZonedDateTime.now();
  22. System.out.println(zdtJvmDefault);
  23. }
  24. }
Success #stdin #stdout 0.17s 58680KB
stdin
Standard input is empty
stdout
2021-07-27T12:47:56.747734-04:00[America/New_York]
2021-07-27T11:47:56.747734-05:00[America/Mexico_City]
2021-07-27T22:17:56.794966+05:30[Asia/Kolkata]
2021-07-27T16:47:56.828407Z[GMT]