File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Calculate maximum number of days in given month Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Calculate maximum number of days in given month" Watch "Calculate maximum number of days in given month" New topic
Author

Calculate maximum number of days in given month

Gaurav Chhabras
Ranch Hand

Joined: Sep 21, 2005
Posts: 126
Hi

I have the date JAN-02-2006 and i have to find out the maximum number of days within this month, like it has 31.
How will i calculate that.

Thanks
Regards
Gaurav
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12952
    
    3

You could do that like this:

- 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);


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32830
    
    4
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.
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
Isn't a simple call to calendar.getMaximum() good enough??


ASCII silly question, Get a silly ANSI.
Michael Duffy
Ranch Hand

Joined: Oct 15, 2005
Posts: 163
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.


%
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35440
    
    9
That's neat. The javadocs had lead me to believe that getMaximum would always return 31, and I had wondered about the use of that.


Android appsImageJ pluginsJava web charts
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
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
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
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calculate maximum number of days in given month
 
Similar Threads
calculating the number of business minutes in a month
Can anyone send me the code to calculate the years/months/days between the tow dates.
Date Calculation in JAVA - Is it really trivial ?
How can i calculate the number of days in year ?
GregorianCalendar - # of work days