• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Bug with java.util.Calendar

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anyone tell me why I am getting Thursday as DAY_Of_Week for 2010-01-04 ( 4th January, 2010) when it was Monday that year?

 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes - not a bug, but a bit of a "gotcha". This is from the Javadocs:

month - the value used to set the MONTH calendar field in the calendar. Month value is 0-based. e.g., 0 for January.


(my emphasis)
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And next time, use Calendar.JANUARY for the first month instead.

Henry
 
Sheriff
Posts: 22783
131
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
And don't use 01 or 04. Those are octal numbers. You will run into problems eventually when using octal numbers without knowing it.
 
Towid Khan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
understood. but if you are getting the data from an outside source, where the convention ( i believe no one think of January as 0th Month on top of their head as oppose to being "1st" month). is "1st" Month is January, there is no way to know the Mapping between constant and Month.

it is not always the case that you are getting Calendar.JANUARY as a parameter. so it is definitely a room for improvement for this class.

i used the octal just for the sake of giving the example.

Thanks everyone
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Towid Khan wrote:but if you are getting the data from an outside source, where the convention ( i believe no one think of January as 0th Month on top of their head as oppose to being "1st" month). is "1st" Month is January, there is no way to know the Mapping between constant and Month.

it is not always the case that you are getting Calendar.JANUARY as a parameter. so it is definitely a room for improvement for this class.



Using the "outside source" argument to say its the core library's fault is... well, what happens if the outside source sends a string "JAN", using a latin font, in EBCDIC, etc. It is your program that has to parse the "outside source" to the format that is document by the library.

Henry
 
Towid Khan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just dont think that it is natural to think of January as 0th Month.

Thanks
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Towid Khan wrote:I just dont think that it is natural to think of January as 0th Month.



Now, this is a different (and fair) argument. If fact, one of the reasons why you got such a fast response is, IMO, because many of us got burned by it before.

Henry
 
Rob Spoor
Sheriff
Posts: 22783
131
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
If you expect a month between 1 and 12, you can use the following two translations:
Note that Calendar.JANUARY is 0 so it could be omitted, but I tend to keep it in case the value would ever change for some reason. Also, it's clearer that month 1 will be mapped to Calendar.JANUARY instead of just subtracting 1.

I use similar code with Calendar.SUNDAY (which is 1... go figure) as well.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic