This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes WEEK_OF_MONTH with alternate result Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "WEEK_OF_MONTH with alternate result" Watch "WEEK_OF_MONTH with alternate result" New topic

WEEK_OF_MONTH with alternate result

peter De Winter

Joined: Jun 03, 2004
Posts: 2

I would like to know how I can find out the WEEK_OF_MONTH value of a date using a GregorianCalendar, but with a special twist.
Consider the following:
//The first day of a week is always monday.
GregorianCalendar x = new GregorianCalendar(104,04,31); // that is monday 31 may 2004 (104 -> 2004, 04 -> may, 31 -> 31)
x.setMinimalDaysInFirstWeek(7); // a week has 7 days

Now this gives me the value "5" (from 5th week of may).

But I need it to return that monday 31 may 2004 lays in the first week of june (I think the return value should be "0" or "1" for the first week) because 31 may lays in the same week as 1 june and therefor is considered being part of the month june.

How can I do this?

Thanks cib
Shane Burgel
Ranch Hand

Joined: Sep 09, 2003
Posts: 47
Well this might be too simple of an idea but what I would do is put a check in to see if the date you are checking is close enough to the end of the month that it might include the first of the next month and if it is return a 1.

ex. if the day you are checking is a tuesday, and the month in question has 31 days, then you know that if the day is the 26th or later, then it will include the first of the next month and you could just return 1 without ever needing to check the WEEK_OF_MONTH
peter De Winter

Joined: Jun 03, 2004
Posts: 2
I thougth so i would have to do it that way.
I wasn' t sure you could do it using methods of the GregorianCalendar class.
Thanks very much for the help.

greets cib
bart zagers
Ranch Hand

Joined: Feb 05, 2003
Posts: 234

you could check of the WEEK_OF_YEAR of your date is the same as the WEEK_OF_YEAR of the first day of the next month.
If so, then it is the first week of the month.
If not, just take the normal WEEK_OF_MONTH of your date.

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
if your monday date could be any monday in the month and you want it's week
number for the month, try something like this (needs a fair bit of testing).

I agree. Here's the link:
subject: WEEK_OF_MONTH with alternate result
It's not a secret anymore!