File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Calendar.set(HOUR_OF_DAY, 1) confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Calendar.set(HOUR_OF_DAY, 1) confusion" Watch "Calendar.set(HOUR_OF_DAY, 1) confusion" New topic
Author

Calendar.set(HOUR_OF_DAY, 1) confusion

Bridget Kennedy
Ranch Hand

Joined: Nov 30, 2004
Posts: 84
I am puzzled by some GregorianCalendar behavior. The code in question:

If I run this program as is, I get the following output:


If I comment out the B and D System.out.println statements, the output is:

My questions are:

1. Why, in the first case, does setting HOUR_OF_DAY have no effect?
2. Why, in the second case, does DAY_OF_MONTH decrement by 1?
3. Why are the C, E outputs different in the two cases?

Thanks for your consideration.
[ April 17, 2008: Message edited by: Bridget Kennedy ]
Rodrigo Lopes
Ranch Hand

Joined: Feb 29, 2008
Posts: 119
I don't know why (at least, not yet), but if you remove the line



then, the result will be the expected.
Bridget Kennedy
Ranch Hand

Joined: Nov 30, 2004
Posts: 84
Another interesting observation, R Lopes, though I can't think of a good reason for the behavior.

It seems there is a lot going on 'behind the scenes'. Mysteries are never a good thing in software development.

Thanks for your interest.
Piyush Porwal
Ranch Hand

Joined: Apr 09, 2008
Posts: 30
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)
Bridget Kennedy
Ranch Hand

Joined: Nov 30, 2004
Posts: 84
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.
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

Bridget,

Your output clearly shows why your calls to

are not changing the hour of the day.

First you are asking it to


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
Bridget Kennedy
Ranch Hand

Joined: Nov 30, 2004
Posts: 84
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.

Thanks again.

[ April 23, 2008: Message edited by: Bridget Kennedy ]
[ April 23, 2008: Message edited by: Bridget Kennedy ]
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

Bridget,

The confusing factor here is that you're using SimpleDateFormat to format your date strings (which you have to, per the API).

SimpleDateFormat will default to your system TimeZone and Locale if you don't specifically set it.

To see how this works, modify your format string to include the time zone:



now, print out the time as is, then set the time zone on the dateFormat, and print your time again:


See what happens to your output.

It's definitely not intuitive that you have to set the TimeZone on both the Calendar and the SimpleDateFormat, but Dates and Times are always tricky.
Bridget Kennedy
Ranch Hand

Joined: Nov 30, 2004
Posts: 84
...and the light switch is turned on.

Thank you very much for your time, Stevi.
Qadeer Ghouri
Greenhorn

Joined: Oct 03, 2011
Posts: 2
Here what I did is added some code
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Qadeer Ghouri wrote:

That line of code won't even compile. There is no such method.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
Consider Paul's rocket mass heater.
 
subject: Calendar.set(HOUR_OF_DAY, 1) confusion
 
Similar Threads
Calendar
How to compare (Hour) with actual new Date() ?
add one day to current date
Calendar Object - updateTime()
current time problem on AIX