Time Zones and java.util.Date

suggest change

A java.util.Date object does not have a concept of time zone.

However, it is possible to display the date represented by the point in time described by the Date object in a different time zone using, for example, java.time.format.DateTimeFormatter:

Date date = Date.from(Instant.parse("2016-07-21T20:50:56Z"));
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");

ZonedDateTime warsaw = date.toInstant().atZone(ZoneId.of("Europe/Warsaw"));
System.out.println(warsaw.getZone());
System.out.println(formatter.format(warsaw));

ZonedDateTime london = date.toInstant().atZone(ZoneId.of("Europe/London"));
System.out.println(formatter.format(london));
Europe/Warsaw
2016-07-21 22:50:56
2016-07-21 21:50:56

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents