Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

epoch is wrong

 
Richard Hayward
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would have expected this code to print:

Thu Jan 01 00:00:00 GMT 1970



But it doesn't. I get:

The epoch: Thu Jan 01 01:00:00 GMT 1970

It's an hour wrong. Anyone know why?


Richard
 
Stephan van Hulst
Bartender
Pie
Posts: 5905
63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably because your default time zone is an hour ahead of Greenwich mean time. Note that Dates represent an absolute point in time, independent from your location. But when you print a Date, it has to convert it into some sort of textual representation, which of course *does* depend on your location.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if you use a SimpleDateFormat that includes a TZ, I bet you'll see that 01:00 in that TZ is 00:00 in GMT.

Apparently I misunderstood the question. Fortunately, others have examined it more closely.
 
Stephan van Hulst
Bartender
Pie
Posts: 5905
63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I notice now that your date string says GMT. The discrepancy might arise from daylight savings which is not displayed in the toString() method. Regardless, like Jeff suggested you should probably always use a DateFormat to print points in time.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daylight savings certainly can be used inside the toString() method of Date - but any time zone labeled as GMT has absolutely no business using daylight savings time. Ever. So something is pretty screwed up there in how the JDK is printing the localized time.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49432
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Probably because your default time zone is an hour ahead of Greenwich mean time. . . .
I remember the epoch. I was around to watch. It was in fact at 1.00am, not midnight, because we had summer time (daylight saving time) all winter that year. It was pretty unpleasant, so in1971 we went back to GMT in winter.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49432
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It was not called GMT, but BST = British Standard Time, back then.
 
Stephan van Hulst
Bartender
Pie
Posts: 5905
63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:Daylight savings certainly can be used inside the toString() method of Date - but any time zone labeled as GMT has absolutely no business using daylight savings time. Ever. So something is pretty screwed up there in how the JDK is printing the localized time.

Good point. I'm at a loss why it prints that the way it prints it.

For me it correctly prints "Thu Jan 01 01:00:00 CET 1970".
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:It was not called GMT, but BST = British Standard Time, back then.

Well, that would certainly confuse things, given what BST means now.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed strange that the hour is off by one while the time zone is GMT.

I tried the following on my system (JDK 1.7.0 update 3, 64-bit, on Ubuntu 11.10):

Output exactly as I'd expect (my local time zone is CET):

Thu Jan 01 01:00:00 CET 1970
1970-01-01 00:00:00 GMT (+0000)
1970-01-01 00:00:00 UTC (+0000)

 
Richard Hayward
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Stephan van Hulst wrote:Probably because your default time zone is an hour ahead of Greenwich mean time. . . .
I remember the epoch. I was around to watch. It was in fact at 1.00am, not midnight, because we had summer time (daylight saving time) all winter that year. It was pretty unpleasant, so in1971 we went back to GMT in winter.


Hi Campbell,

Apologies for being thick, but is this an explanation for the Date(0) being an hour wrong, or is it just banter?

Richard
 
Richard Hayward
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Indeed strange that the hour is off by one while the time zone is GMT.

I tried the following on my system (JDK 1.7.0 update 3, 64-bit, on Ubuntu 11.10):

Output exactly as I'd expect (my local time zone is CET):

Thu Jan 01 01:00:00 CET 1970
1970-01-01 00:00:00 GMT (+0000)
1970-01-01 00:00:00 UTC (+0000)



When I first posted, I was testing on XP with java version 1.7.0_03.
I've now tested, like you did on Ubuntu 11.10 with java 1.7.0_3, although 32 bit,
but I get the same discrepancy as before.

Interestingly, if I make a DateFormat and ask it's TimeZone, I get GMT. But Date(0) is an hour wrong.
If I then explicitly set the TimeZone to GMT, as you did, it gets Date(0) correct. Odd...




richard@Shakespeare:~/projects/java/classes$ java EpochTest
System's Timezone: Greenwich Mean Time
TimeZone of newly created DateFormat = Greenwich Mean Time
Thursday, 1 January 1970 01:00:00 o'clock GMT
Thursday, 1 January 1970 00:00:00 o'clock GMT



Regards
Richard
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's really strange, as if there are two variants of the time zone "Greenwich Mean Time"? This is either a bug in Java, or there is some special reason for this that we don't understand.

I wrote a small program using Joda Time, an alternate date and time library for Java (with a much better designed API than Java's standard Calendar and Date classes).

It doesn't show an offset of an hour on my system:

1970-01-01 00:00:00 +00:00 (+0000)

 
Campbell Ritchie
Sheriff
Pie
Posts: 49432
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guy Hayward wrote: . . .

Apologies for being thick, but is this an explanation for the Date(0) being an hour wrong, or is it just banter?

. . .
Half and half. If you were in a GMT area (Britain) the epoch fell at 1.00am. In other places, eg parts of Africa, which are in the GMT zone, it would have been at midnight. The fact that you are showing GMT might suggest an error. I managed to get 1.00am from System.out.println(new Date(0L)); so the behaviour is at least reproducible; that would appear to be the same as what Jesper did. Whether our having summer time all winter in 1970 is the explanation, I am not sure. It does say midnight here.
 
Richard Hayward
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all for your help on this thread.

I think I'll have to leave it as an unexplained mystery for the moment.

Regards
Richard
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic