DateFormat prints a new date (0) as an era + 1 hour

The following test failed:

DateFormat df = new SimpleDateFormat("HH:mm:ss z"); assertEquals("00:00:00 GMT", df.format(new Date(0))); 

expected "00:00:00 GMT", but was "01:00:00 GMT"

Can someone please indicate where I am stupid, please?

I spent more time on it than it would take to simply replace everything with Joda-Time. There's a lesson somewhere.

+6
source share
2 answers

The problem is that Java has an error in the name of the time zone abbreviation for Europe and London in 1970.

In the winter of 1970, the United Kingdom was still at UTC + 1, but Java believes that it is still called GMT. So, what you see is local time at midnight UTC on January 1, 1970 ... it's just that we're used to GMT = UTC, which is why it is confusing.

(As a side note, it’s still worth switching to Joda Time anyway. Avoid built-in libraries like the plague :)

Joda Time prints the abbreviated time in the time zone as "BST". It seems just as bizarre that it’s clear that it wasn’t summer time ... but it’s possible that the “S” here is the standards for Standard, as it was during the “British Standard Time Experiment”. See Wikipedia for more details.

(Note that UTC itself did not even exist in the Unix era - it was introduced in 1972. I accept proletic UTC for this answer :)

+11
source

new Date (0) returns Selects a Date object and initializes it to represent the specified number of milliseconds since the standard base time, known as the "era", namely January 1, 1970, 00:00:00 GMT.

So, the value will be Thu Jan 01 05:30:00 IST 1970

"05:30:00" Suitable for your location

0
source

Source: https://habr.com/ru/post/897254/


All Articles