aspose file tools*
The moose likes Java in General and the fly likes Bug with java.util.Calendar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Bug with java.util.Calendar" Watch "Bug with java.util.Calendar" New topic
Author

Bug with java.util.Calendar

Towid Khan
Greenhorn

Joined: Apr 12, 2008
Posts: 8
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

Joined: Apr 06, 2010
Posts: 4490
    
    8

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
Sheriff

Joined: Sep 28, 2004
Posts: 19064
    
  40

And next time, use Calendar.JANUARY for the first month instead.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

And don't use 01 or 04. Those are octal numbers. You will run into problems eventually when using octal numbers without knowing it.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Towid Khan
Greenhorn

Joined: Apr 12, 2008
Posts: 8
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
Sheriff

Joined: Sep 28, 2004
Posts: 19064
    
  40

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

Joined: Apr 12, 2008
Posts: 8
I just dont think that it is natural to think of January as 0th Month.

Thanks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19064
    
  40

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

Joined: Oct 27, 2005
Posts: 19784
    
  20

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Bug with java.util.Calendar