This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I don't know why (at least, not yet), but if you remove the line
This is the reason: For your given code, setTimeZone will not re-calculate the time stored within the object, it will just set another object which will point to "GMT". a call to get(HOUR) will re-adjust the values for the new timezone you set (GMT here) (lazy way).
So things went in this way for your code: 1. set time will set internal object to 1196732402000 2. set time zone will set only the object to hold zone = GMT 3. call to calendar.getTime in A. will get time stored (1196732402000)
Output I got with the given code: A. before GregorianCalendar.set HOUR call: 2007-12-04 07:10:02 B. hour of day = 1 C. after first set HOUR_OF_DAY, 1 call 2007-12-04 07:10:02 D. hour of day = 1 E. after get HOUR_OF_DAY and 2nd set HOUR_OF_DAY, 1 calls: 2007-12-04 07:10:02
Looks like dependent on local timezone?
-Piyush Porwal (Junk box)
Joined: Nov 30, 2004
Thanks for the reply, Piyush.
I still don't understand why the set HOUR has no effect.
It seems I have a fundamental misundertanding about how the Calendar class works.
This displays the current value of the field. What value does it display? What value are you then setting the Calendar.HOUR_OF_DAY field to?
What if you change the call to
Does that change the results?
The Calendar API is very tricky.
There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
Joined: Nov 30, 2004
Thank you for your patient response, Stevi.
Sorry to be so dense. I does make sense that HOUR_OF_DAY would not change from 1 to 1 when when I'm setting it to 1. However, I do not understand why HOUR_OF_DAY = 1 in the first place when it looks to me like it should be 20. Nor do I understand, in the second case, why day of month decreases by 1 in between the A and C printlns.
[ April 23, 2008: Message edited by: Bridget Kennedy ] [ April 23, 2008: Message edited by: Bridget Kennedy ]