- Create a Calendar object and set it to the first of the next month (for example, to 1 February 2006). - Subtract 1 day off the calendar: cal.add(Calendar.DATE, -1); - Get the day number of the calendar: int numberOfDaysInJanuary = cal.get(Calendar.DATE);
Do you mean you want a method which calculates number of days in the month? Here is a totally diffrent approach:
Remember that you don't need a break; statement after a return; statement. Also that the Date and Calendar classes of java.util use JANUARY = 0, DECEMBER = 11, etc.
Originally posted by Campbell Ritchie: Do you mean you want a method which calculates number of days in the month? Here is a totally diffrent approach:
Remember that you don't need a break; statement after a return; statement. Also that the Date and Calendar classes of java.util use JANUARY = 0, DECEMBER = 11, etc.
So why do you repeat those static definitions in your class?
Too much code - why add to your maintainence burden? I like the getMaximum() call idea much better, indeed.
Indeed, getMaximum() should return 31. It's getActualMaximum() that you want here.
"I'm not back." - Bill Harding, Twister
Gaurav Chhabras
Ranch Hand
Joined: Sep 21, 2005
Posts: 126
posted
0
Hey
Thanks a lot to you all for your instant replies. I have found the solution for that as follows :
SimpleDateFormat df = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss"); String sdate1 = "1-1-2004 00:00:00"; Date date1 = df.parse(sdate1); Calendar cal1 = Calendar.getInstance(); cal1.setTime(date1);
maximum1 = cal1.getActualMaximum(cal1.DAY_OF_MONTH); // This will return the maximum.
If any queries then feel free to ask.
Thanks Regards Gaurav
Stuart Ash
Ranch Hand
Joined: Oct 07, 2005
Posts: 637
posted
0
Originally posted by Jim Yingst: Indeed, getMaximum() should return 31. It's getActualMaximum() that you want here.
Actually, I should've been clearer about this. I had been perusing this API doc with an intent to get the answer to this question sometime ago, and I wasn't sure which of the getXMaximum() methods would do the job, and hadn't tried. Glad we now have a try and its results.