• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

Calendar.set(HOUR_OF_DAY, 1) confusion

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Bridget Kennedy
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bridget Kennedy
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...and the light switch is turned on.

Thank you very much for your time, Stevi.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here what I did is added some code
 
Sheriff
Posts: 22728
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Qadeer Ghouri wrote:


That line of code won't even compile. There is no such method.
 
Don't sweat petty things, or pet sweaty things. But cuddle this tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic