This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes epoch is wrong Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "epoch is wrong" Watch "epoch is wrong" New topic
Author

epoch is wrong

Richard Hayward
Ranch Hand

Joined: Feb 15, 2012
Posts: 68
    
    1
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

Joined: Sep 20, 2010
Posts: 3617
    
  14

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Sep 20, 2010
Posts: 3617
    
  14

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

Joined: Mar 05, 2008
Posts: 3013
    
  10
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

Joined: Oct 13, 2005
Posts: 38519
    
  23
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

Joined: Oct 13, 2005
Posts: 38519
    
  23
It was not called GMT, but BST = British Standard Time, back then.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3617
    
  14

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

Joined: Mar 05, 2008
Posts: 3013
    
  10
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

Joined: Aug 16, 2005
Posts: 14117
    
  16

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)


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Richard Hayward
Ranch Hand

Joined: Feb 15, 2012
Posts: 68
    
    1
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

Joined: Feb 15, 2012
Posts: 68
    
    1
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

Joined: Aug 16, 2005
Posts: 14117
    
  16

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

Joined: Oct 13, 2005
Posts: 38519
    
  23
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

Joined: Feb 15, 2012
Posts: 68
    
    1
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
 
wood burning stoves
 
subject: epoch is wrong