tags:

views:

97

answers:

3

My problem is pretty straigtforward explained :

if i do this :

public class Main {

public static void main(String[] args) throws Exception {
        Date d = new Date(0L );
        System.out.println(d);
}

}

I get the following output : Thu Jan 01 01:00:00 CET 1970

According to the doc, i was expecting : Thu Jan 01 00:00:00 CET 1970

I would like was going wrong...

EDIT : Indeed, i read the doc too fast. I sould have Thu Jan 01 00:00:00 GMT 1970

So, how can i force the use of GMT, and ignore all local time ?

Edit, Solution :

public static void main(String[] args) throws Exception {
    SimpleDateFormat sdf = new SimpleDateFormat("H:m:s:S");
SimpleTimeZone tz = new SimpleTimeZone(0,"ID");
sdf.setTimeZone(tz) ;
Date d = new Date(0L );
System.out.println( sdf.format(d));
}
+1  A: 

CET is one hour ahead of GMT, which is the time zone used to define the Epoch.

Jim Lewis
+2  A: 

This might be related to your locale settings. Assuming you're French and not French-Canadian, it would seem as if your timestamp is being treated as timestamp without timezone, and the Date constructor attempts to correct for this, adding an hour to the date.

If this is undocumented behavior or not, I cannot tell you.

Edit: Read error: CET != UTC :/

So yeah, Locale time zone.

Reedit: For utter and absolute clarity.

Output: Thu Jan 01 01:00:00 CET 1970

Your expected output: Thu Jan 01 00:00:00 CET 1970

Actual expected output: Thu Jan 01 00:00:00 GMT 1970 ( ≡ Thu Jan 01 01:00:00 CET 1970)

Williham Totland
Indeed, i'm french...
Antoine Claval
Well, it's not so much about your nationality, more about what timezone to which your clock is set, if you know what I mean. ;)
Williham Totland
+5  A: 

The Epoch is defined as 00:00:00 on 1970-1-1 UTC. Since CET is UTC+1, it's equal to 1AM your time.

If you look at the Date(long) constructor, you'll see that it expects the value to be the number of milliseconds since the epoch, UTC:

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.

Regarding your desire to force GMT instead of your local time zone: In short, the Date instance always uses GMT. If you just want to format the output String so that it uses GMT have a the DateFormat class, and specifically, its setTimeZone() method.

Jason Nichols
Ok, but i dont see anything in the creation of the Date object to ignore local time, or to force the GMT
Antoine Claval
The date is always stored as an offset from the epoch GMT. The default toString() format takes into account your local timezone, but that's only for presentation. Internally it's simply the offset from the epoch.
Jason Nichols