3

The java.util.Date class is based on the number of seconds since 1 January 1970 00:00 GMT. So why does this code

System.out.println(new Date(0));

print Thu Jan 01 01:00:00 GMT 1970? My local time zone is GMT, so I expected it to print 00:00:00 GMT.

DodgyCodeException
  • 5,963
  • 3
  • 21
  • 42

3 Answers3

5

There is an interesting reason for this. Refer (BST Offset bug report) . It says, "and the experiment with British Standard Time from 1968 to 1972, by which the time was advanced by one hour from GMT throughout the year." And further: “The local time produced by Date.toString() is historically correct, except for the time zone abbreviation. It should be "BST" (British Standard Time for this case), but it's a known limitation of the current TimeZone implementation.”

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
CGS
  • 2,782
  • 18
  • 25
  • 1
    Phantastic EU reminiscent fact. – Joop Eggen Feb 23 '18 at 10:45
  • 1
    Interesting tidbit of history, but it does not explain why the 1 AM in *GMT* is displayed. Your answer would explain a display of *BST*, but that is not what the Question reports. – Basil Bourque Feb 24 '18 at 05:17
  • 1
    @BasilBourque, correct, I edited the answer. Interestingly the bug is in the modern API too. `Instant.EPOCH.atZone(ZoneId.of("Europe/London")).format(DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ROOT))` too produces `Thu Jan 01 01:00:00 GMT 1970` on my Java 9. – Ole V.V. Feb 25 '18 at 12:52
1

This link might help. I'm quite a novice at the Date class, but I figured this could help somehow.

Robo Mop
  • 3,485
  • 1
  • 10
  • 23
-1

Unix Epoch Time is a system of time describing how much time has elapsed since January 1st, 1970.

Therefore, when you create a new java.util.Date object with 0 milliseconds elapsed, it will return January 1st, 1970.

What you are looking for is here.

Austin Schaefer
  • 695
  • 5
  • 19