• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Calender

 
accnit Jai
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing the logic around the java Calender. I have following requirements.

Working on day of week calender which contains 4 weeks in a month. Need to select Day of a week for a month. If selected week day is passed then for next month same week day has to be selected.
If a month start on Saturday then second Monday will be 10th of that month rather than 3rd.

Please let me know if there are any utility is available in java to achieve above or any sample code will be much appreciated.
 
Emanuel Kadziela
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try Joda Time. It's a library providing much more functionality than the basic date time stuff in java. Not sure if your specific requirements are met, but it should help.
 
Tony Docherty
Bartender
Posts: 2965
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
accnit Jai wrote: Working on day of week calender which contains 4 weeks in a month. Need to select Day of a week for a month. If selected week day is passed then for next month same week day has to be selected.
If a month start on Saturday then second Monday will be 10th of that month rather than 3rd.

I'm sorry but I'm not sure I understand what you mean by this.
I can tell you the Calendar class has methods to get and set this type of information (eg Day of week, Day of Month, Day of week in Month etc). Alternatively you can try Joda time which has lots of useful date handling features.
 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
accnit Jai wrote:Please let me know if there are any utility is available in java to achieve above or any sample code will be much appreciated.

Like the others, I'd suggest looking at Joda Time; but only AFTER you've worked out how to do it for yourself.

What you want is perfectly easily obtainable from the Java GregorianCalendar class because, like it or not, Calendar's DAY_OF_WEEK field is a number (from 1 for Sunday through 7 for Saturday). Combine that with DAY_OF_WEEK_IN_MONTH and you have a simple way of getting the correct day.

My question to you is: what do you do about getting the 5th X-day of a month? There are several months that have them.

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic