Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

WEEK_OF_MONTH with alternate result

 
peter De Winter
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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.setFirstDayOfWeek(java.util.Calendar.MONDAY);
x.setMinimalDaysInFirstWeek(7); // a week has 7 days
System.out.println(x.get(com.kbc.alg.ifs.d0.model.Datum.WEEK_OF_MONTH));

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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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.

Bart.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic