Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calendar : getting week of month

 
Gouri Bargi
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to get the dates for a particular week in a month.
I am trying to use set(Calendar.WEEK_OF_MONTH) and it is giving me strange results. Here is the code:

The output is
Fri Mar 12 10:50:59 GMT+05:30 2004
As 1st March 2003 is a Monday, the next week date I am expecting is 8th march. Can anyone help?
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can try change 'date.set(Calendar.WEEK_OF_MONTH, 2);' to: 'date.add(Calendar.WEEK_OF_MONTH, 1);' witch add one week to the current Date.
Ren´┐Ż
 
Gouri Bargi
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rene. It works. Meanwhile, I came across another alternative - if I modify the code as following, it works:

And another alternative - If I add a call to
date.get(Calendar.WEEK_OF_MONTH) before setting the week of month, I get the desired output:

Is this a bug?
 
Eddie Vanda
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could try the "complete ()" method before using the values. From the api docco:

protected void complete();
Fills in any unset fields in the calendar fields. First, the computeTime() method is called if the time value (millisecond offset from the Epoch) has not been calculated from calendar field values. Then, the computeFields() method is called to calculate all calendar field values.

[ March 12, 2004: Message edited by: Eddie Vanda ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic