jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes Java calendar issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java calendar issue" Watch "Java calendar issue" New topic

Java calendar issue

Jeff Vybihal

Joined: Sep 12, 2011
Posts: 2
I'm trying to figure out how to calculate the number of days between two specified dates but only on certain days of the week. The code below should work however, after I set the variable current to 7/1/2011 I then display the value of current and it doesn't reflect what day of the week it should really be.

For example, 7/1/2011 falls on a Friday or integer value 6. If you run the code shown below, what you see is the value 2 (Monday) being returned.

I am determining the integer values based on the Calendar API


Any ideas?
Paul Clapham

Joined: Oct 14, 2005
Posts: 19747

When you use "7" in that position it means "August", not July. You aren't passing a month number, you are passing one of the Calendar constants. In this case you are passing Calendar.AUGUST, whose value is 7. Change the program to pass Calendar.JULY and see what happens.
Jeff Vybihal

Joined: Sep 12, 2011
Posts: 2
Thanks for the speedy reply. This greenhorn missed that key information!

Thanks again!
Rob Spoor

Joined: Oct 27, 2005
Posts: 20132

It's an often made mistake, and I blame Sun for this. They chose to let weekdays start at 1 (Calendar.SUNDAY), which is OK. Yet they chose to let months, which naturally start at 1, start at 0 (Calendar.JANUARY). This makes no sense whatsoever. They should either have made both start at 0 (to be consistent) or both start at 1 (again, to be consistent, but also to be more friendly).

How To Ask Questions How To Answer Questions
I agree. Here's the link: http://aspose.com/file-tools
subject: Java calendar issue
It's not a secret anymore!