fork download
  1. import java.time.ZoneId;
  2. import java.time.ZonedDateTime;
  3.  
  4. public class Main {
  5. public static void main(String[] args) {
  6. // ZonedDateTime.now() is same as ZonedDateTime.now(ZoneId.systemDefault()). In
  7. // order to specify a specific timezone, use ZoneId.of(...) e.g.
  8. // ZonedDateTime.now(ZoneId.of("Europe/London"));
  9. ZonedDateTime zdtDefaultTz = ZonedDateTime.now();
  10. System.out.println(zdtDefaultTz);
  11.  
  12. // Convert zdtDefaultTz to a ZonedDateTime in another timezone e.g.
  13. // to ZoneId.of("America/New_York")
  14. ZonedDateTime zdtNewYork = zdtDefaultTz.withZoneSameInstant(ZoneId.of("America/New_York"));
  15. System.out.println(zdtNewYork);
  16. }
  17. }
Success #stdin #stdout 0.13s 54780KB
stdin
Standard input is empty
stdout
2021-07-25T14:48:38.984246Z[GMT]
2021-07-25T10:48:38.984246-04:00[America/New_York]