• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Bug with java.util.Calendar

 
Towid Khan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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?

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • 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)
 
Henry Wong
author
Marshal
Pie
Posts: 22094
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And next time, use Calendar.JANUARY for the first month instead.

Henry
 
Rob Spoor
Sheriff
Pie
Posts: 20750
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Marshal
Pie
Posts: 22094
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
I just dont think that it is natural to think of January as 0th Month.

Thanks
 
Henry Wong
author
Marshal
Pie
Posts: 22094
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 20750
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic