user718146
user718146

Reputation: 400

A bizarre phenomenon in my java program

I found a bizarre phenomenon in my java program at runtime, just look at my code:

System.out.println(" date " + new Date(1359931355141L).toGMTString() );

the output by this statement was "date 3 Feb 2013 22:42:35 GMT", and

System.out.println(" date " + new Date(1359931355141L).getDate() );

the output by this was "date 4" , see, why not 3 here ???

I could not figure out what's wrong with my program; I am doubting whether my JVM ran into bugs.
Guys, would you like to have a test on your JVM for this two statements?

Upvotes: 3

Views: 174

Answers (3)

Adi
Adi

Reputation: 2394

Well. that depends on what your current locale is. GMT will give you time if GMT timezone. getDate will give you time of your locale.

Upvotes: 0

Bohemian
Bohemian

Reputation: 425033

Because you live east of central Europe (in a timezone that is at least GMT+1.5Hr).

getDate() (which is deprecated btw) returns the day of the month, and it's returning 4 (instead of 3) because in your timezone, that epoch time is already into the next day, whereas in England (GMT) it's still day 3 of the month.

Upvotes: 12

gerrytan
gerrytan

Reputation: 41123

What's your locale? If it's 3 Feb 2013 22:42:35 GMT but your locale is GMT+10 then your local date will be 4. Nothing bizzare at all, this is the expected behavior

Upvotes: 1

Related Questions