File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Java Calender Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Calender " Watch "Java Calender " New topic
Author

Java Calender

accnit Jai
Ranch Hand

Joined: Feb 15, 2011
Posts: 33
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

Joined: Mar 24, 2005
Posts: 186
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

Joined: Aug 07, 2007
Posts: 2232
    
  47
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

Joined: Mar 17, 2011
Posts: 7639
    
  19

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
Consider Paul's rocket mass heater.
 
subject: Java Calender