Time Zones and java.util.Date
suggest changeA java.util.Date object does not have a concept of time zone.
- There is no way to set a timezone for a Date
- There is no way to change the timezone of a Date object
- A
Dateobject created with thenew Date()default constructor represents the instant at which it was created
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
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents