aspose file tools*
The moose likes Java in General and the fly likes Calendar() / Date()   bug ??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Calendar() / Date()   bug ???" Watch "Calendar() / Date()   bug ???" New topic
Author

Calendar() / Date() bug ???

Paula Wellera
Greenhorn

Joined: Jan 08, 2004
Posts: 3
Hy folks,
I'm somewhat confused, hope somebody can answer my question.
Why do I get this result:
Code:
-----
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(0);
Date testdate = calendar.getTime();
System.out.println(testdate +" millis: "+ testdate.getTime());
Output:
-------
Thu Jan 01 01:00:00 GMT 1970 millis: 0

Shouldn't I get "Thu Jan 01 00:00:00 GMT 1970" ???
What's going on here? Sure, I'm in GMT.
Thanks for any idea in advance,
emmzettel
Jacky Chow
Ranch Hand

Joined: Sep 01, 2002
Posts: 63
hi Paula,
This is not a bug, the time of Java programming is always started at "Thu Jan 01 00:00:00 GMT 1970", it is mentioned in Java spec.


Jacky
Angel Dobbs-Sciortino
Ranch Hand

Joined: Sep 10, 2003
Posts: 101
But according to her post, she's getting 01:00:00 as the time, not 00:00:00.
Angel
Jacky Chow
Ranch Hand

Joined: Sep 01, 2002
Posts: 63
Originally posted by Angel Dobbs-Sciortino:
But according to her post, she's getting 01:00:00 as the time, not 00:00:00.
Angel

Oh, sorry, this is my bug
If the time is 01:00:00, I think the time zone should be GMT+01, but time zone of the output is not GMT+01 then...
Paula, I suggest that you try to test your code with different time zone!
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
Originally posted by Paula Wellera:
Hy folks,
I'm somewhat confused, hope somebody can answer my question.
Why do I get this result:

Paula, Welcome to the JavaRanch!!!
What Jacky Chow said is correct... It is because of your Time Zone set in your system... I can say that you are using European Time Zone, which is one hour ahead Greenwich Standard Time...
Hope this helps...


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Paula Wellera
Greenhorn

Joined: Jan 08, 2004
Posts: 3
Hello again,
Well Ko Ko, I'm sitting in germany ;-) .It took me 2 hours to find the reason. It's not the timezone setting on my machine as you suggest. When I posted the message I was already playing with different timezones around the world. Always the same result.
The reason is the TimeZone implementation "GMT" aka "Europe/London".
Even if you are in GMT you get different results for
Date.toString() // Thu Jan 01 01:00:00 GMT 1970
and
Date.toGMTString() // Thu Jan 01 00:00:00 GMT 1970 (depr.)
The 1st of january is definitely not in any daylightsavingtime (as TimeZone.inDaylightTime(Date date) shows. Despite this fact the toString() method still shows 01:00:00. May be it is some strange british use/behaviour/history I don't know. When it shows GMT it should be GMT and this implementation doesn't.
Thanks for your posts folks,
Best regards,
Paula
[ January 09, 2004: Message edited by: Paula Wellera ]
Paula Wellera
Greenhorn

Joined: Jan 08, 2004
Posts: 3
Some funny examples:
--------------------
Code:
-----
System.out.println("local time: "+ new Date(System.currentTimeMillis()));
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTimeInMillis(0);
System.out.println("local calendar time: "+ calendar.getTime());
System.out.println("GMT calendar time: "+ calendar.getTime().toGMTString());

Output when in GMT timezone:
----------------------------
local time: Fri Jan 09 19:05:43 GMT 2004
local calendar time: Thu Jan 01 01:00:00 GMT 1970
GMT calendar time: 1 Jan 1970 00:00:00 GMT

Output when in CET timezone (GMT+1)
-----------------------------------
local time: Fri Jan 09 20:06:15 CET 2004
local calendar time: Thu Jan 01 01:00:00 CET 1970
GMT calendar time: 1 Jan 1970 00:00:00 GMT

Code:
-----
long ONE_DAY = 24*60*60*1000;
for (int i=0; i<3000; i++) {
calendar.setTimeInMillis(i * ONE_DAY);
System.out.println("local calendar time: "+ calendar.getTime());
}
Output:
-------
local calendar time: Thu Jan 01 01:00:00 GMT 1970
local calendar time: Fri Jan 02 01:00:00 GMT 1970
local calendar time: Sat Jan 03 01:00:00 GMT 1970
local calendar time: Sun Jan 04 01:00:00 GMT 1970 any time but not GMT
local calendar time: Mon Jan 05 01:00:00 GMT 1970
local calendar time: Tue Jan 06 01:00:00 GMT 1970
local calendar time: Wed Jan 07 01:00:00 GMT 1970
local calendar time: Thu Jan 08 01:00:00 GMT 1970
...
...
local calendar time: Wed Oct 27 01:00:00 GMT 1971
local calendar time: Thu Oct 28 01:00:00 GMT 1971
local calendar time: Fri Oct 29 01:00:00 GMT 1971
local calendar time: Sat Oct 30 01:00:00 GMT 1971
local calendar time: Sun Oct 31 01:00:00 GMT 1971 suddenly it's right
local calendar time: Mon Nov 01 00:00:00 GMT 1971
local calendar time: Tue Nov 02 00:00:00 GMT 1971
local calendar time: Wed Nov 03 00:00:00 GMT 1971
...
...
local calendar time: Thu Mar 16 00:00:00 GMT 1972
local calendar time: Fri Mar 17 00:00:00 GMT 1972
local calendar time: Sat Mar 18 00:00:00 GMT 1972
local calendar time: Sun Mar 19 00:00:00 GMT 1972
local calendar time: Mon Mar 20 01:00:00 BST 1972 brasilian show time ?? british summer time ??
local calendar time: Tue Mar 21 01:00:00 BST 1972
local calendar time: Wed Mar 22 01:00:00 BST 1972
local calendar time: Thu Mar 23 01:00:00 BST 1972

What this means is that most of the years 70 and 71 (how long before 70) there was a time difference to GMT in the GMT's own country. Is that right and if so why?
Anyway, imho naming this GMT is not correct. So one has to be careful with Date.
Any ideas ;-)
Paula
[ January 09, 2004: Message edited by: Paula Wellera ]
Jacky Chow
Ranch Hand

Joined: Sep 01, 2002
Posts: 63
hi Paula,
I found a bug report here(you need to be a member to access):
http://developer.java.sun.com/developer/bugParade/bugs/4832236.html
the bug likes the situation you face, but not exactly the same!
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
The reason is the TimeZone implementation "GMT" aka "Europe/London".
Apparently not. Europe/London is not the same as GMT - definitely not in the summer. Usally it's the same in winter, except when weird laws are in effect. I take it you're doing something like
TimeZone.getTimeZone("Europe/London")
rather than
TimeZone.getTimeZone("GMT")
?
The latter would in fact give you GMT, correctly, as far as I know. The former will give you whatever London was using at the time. It's historiclly correct except for the label "GMT", which is flat-out wrong in this case. The part of this which is Java's fault is described here.
local calendar time: Mon Mar 20 01:00:00 BST 1972 brasilian show time ?? british summer time ??
British Summer Time in this case. From 1968-1971 BST would've meant British Standard Time, which was like British Summer Time all year round. This is the period that Java's TimeZone (or rather ZoneInfo) is mislabeling as GMT. But calling it BST would've been confusing too since most people would think you mean Summer time, and for discussing Jan 1 that doesn't make much sense. Though at least that would alert people looking that the date that something weird was going on.
What this means is that most of the years 70 and 71 (how long before 70) there was a time difference to GMT in the GMT's own country. Is that right and if so why?
Because they were on crack? Well given the time period it was probably a different drug involved. But it's a fallacy to assume that people in Britain use GMT; frequently, they don't. Every summer, for example. :roll:
So - if you ask Java for GMT, you get GMT. But if you ask for London time, you may get something which is mislabeled as GMT. It's not clear whether this is better of worse than what Britain was actually using at the time.
Incidentally toGMTString() is technically deprecated, though I'm not sure why since it still seems to give correct results. Maybe there's another loophole out there somewhere. But TimeZone.getTimeZone("GMT") is officially the preferred way to get GMT.


"I'm not back." - Bill Harding, Twister
 
 
subject: Calendar() / Date() bug ???