fork download
  1. import java.time.*;
  2.  
  3. public class Main {
  4. public static void main(String[] args) {
  5. ZonedDateTime zdt = ZonedDateTime.of(
  6. LocalDate.of(2011, Month.MARCH, 27),
  7. LocalTime.of(2, 0),
  8. ZoneId.of("Europe/Prague")
  9. );
  10. System.out.println(zdt);
  11.  
  12. // If you want to convert to a different zone, use withZoneSameInstant
  13. ZonedDateTime zdt2 = zdt.withZoneSameInstant(ZoneId.of("Etc/UTC"));
  14. System.out.println(zdt2);
  15.  
  16. // However, there is another way to do it for UTC. You can obtain an Instant
  17. // from a ZonedDateTime. An Instant is a point in time independent of any
  18. // time zone. But for practical purposes, we need a reference time zone.
  19. // UTC is used as the reference.
  20. Instant instant = zdt.toInstant();
  21. System.out.println(instant);
  22. }
  23. }
Success #stdin #stdout 0.15s 59384KB
stdin
Standard input is empty
stdout
2011-03-27T03:00+02:00[Europe/Prague]
2011-03-27T01:00Z[Etc/UTC]
2011-03-27T01:00:00Z