开发者

DateFormat is printing new Date(0) as epoch + 1 hour

The following test fails:

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"

开发者_如何学运维

Could someone point out where I'm being stupid please?

I've spent longer looking at this than is would have taken me to just replace everything with Joda-Time. There's a lesson there somewhere.


The problem is that Java has a bug around the name of the Europe/London time zone abbreviation in 1970.

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

(As a side note, it would still be worth converting to Joda Time even now. Avoid the built-in libraries like the plague :)

Joda Time prints the time zone abbreviation as "BST". This seems equally bizarre given that it clearly wasn't summer time... but it's possible that the "S" here standards for Standard, as that was during the period of the "British Standard Time experiment". See Wikipedia for details.

(Note that UTC itself didn't even exist at the Unix epoch - it was introduced in 1972. I'm assuming a proleptic UTC for the sake of this answer :)


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

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

"05:30:00" Depens on your location

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜