Why BST and GMT in your output?
java.util.Date does not hold timezone information. It stores the number of milliseconds since Midnight, January 1st, 1970 UTC and Date#toString applies the JVM's default time zone to it to form the output text. Since, your JVM has Europe/London as the time zone set, Date#toString uses the same.
- August falls under DST in the Europe/London time zone and the time during DST is called British Summer Time (BST). November falls outside DST in the Europe/London time zone and the time outside DST is called Greenwich Mean Time (GMT). Check this and this link to learn more about it.
java.time
In March 2014, Java 8 introduced the modern, java.time date-time API which supplanted the error-prone legacy java.util date-time API. Any new code should use the java.time API.
java.time.ZonedDateTime holds time zone information along with date and time. Below is an excerpt from its documentation:
A date-time with a time-zone in the ISO-8601 calendar system, such as
2007-12-03T10:15:30+01:00 Europe/Paris.
The best thing about it is it automatically adjusts the zone offset as per the DST as you can see in the below demo.
To avoid any misleading output, ZonedDateTime requires you to specify the ZoneId while converting an Instant into a ZonedDateTime.
Demo:
class Main {
public static void main(String[] args) {
ZoneId zoneId = ZoneId.of("Europe/London");
ZonedDateTime zdt1 = Instant.ofEpochMilli(1282863600000L)
.atZone(zoneId);
ZonedDateTime zdt2 = Instant.ofEpochMilli(1321919999000L)
.atZone(zoneId);
System.out.println(zdt1);
System.out.println(zdt2);
}
}
Output:
2010-08-27T00:00+01:00[Europe/London]
2011-11-21T23:59:59Z[Europe/London]
Z represents a time zone offset of +00:00, which is the zone offset for UTC.
Online Demo
Learn more about the modern Date-Time API from Trail: Date Time.
Dateclass was poorly designed and is long outdated, so better avoided.toString()conversion feature