Home arrow static arrow Java Programming [Archive] - Getting first and last dates of a month
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Getting first and last dates of a month
This topic has 5 replies on 1 page.

Posts:32
Registered: 3/3/04
Getting first and last dates of a month  
Apr 20, 2004 1:45 AM



 
Jello all,

I am generating monthly, quartelry and yearly reports, for which I need to get first and last dates of a given Month taking in to account leap year. I searched the API and docs for Calendar class. I could not find any useful methods / fields. I wonder this being a very common requirement, how SUN has not provided these essential features. I still feel, it must be some where. Please help me and reply. Also if you came across any other forum for util package, let me know.
 

Posts:964
Registered: 7/23/03
Re: Getting first and last dates of a month  
Apr 20, 2004 2:06 AM (reply 1 of 5)



 
what i always do is that :
1st of every months will of course the 1st date for every months.
to get the last day of the month, i will try to find 1st for next month and then minus 1 day. This should get the last day of the month.
:)
 

Posts:8,604
Registered: 29/04/03
Re: Getting first and last dates of a month  
Apr 20, 2004 2:50 AM (reply 2 of 5)



 
You can also use getActualMaximum, getActualMinimum calls.

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");Calendar c = Calendar.getInstance();c.set(Calendar.MONTH, Calendar.JANUARY); // Output first of monthc.set(Calendar.DAY_OF_MONTH, 1);System.out.println(sdf.Format(c.getTime());// Output last day of Monthc.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));System.out.println(sdf.Format(c.getTime());
 

Posts:32
Registered: 3/3/04
Re: Getting first and last dates of a month  
Apr 20, 2004 10:00 PM (reply 3 of 5)



 
Thanks evnafets,

Your reply is very specific and answering my query in the best way. I think, when we have appropriate methods like getActualMaximum() etc., why need go to next month's first date and minus 1. This is bit complicated approach.

Thanks once again.

varde
 

Posts:13
Registered: 10/27/03
WARNING  
Jun 17, 2004 12:35 AM (reply 4 of 5)



 
WARNING => WHEN I USE THIS CODE WITH FEBRUARY, JAVA GIVES ME 31

// Output first of month
c.set(Calendar.DAY_OF_MONTH, 1);
System.out.println(sdf.Format(c.getTime());
// Output last day of Month
c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
System.out.println(sdf.Format(c.getTime());

B@ron {EU.BELGIUM}
 

Posts:580
Registered: 8/21/02
Re: WARNING  
Jun 17, 2004 1:32 AM (reply 5 of 5)



 
WARNING! You don't use the Calendar class correctlly! Run this:
    Calendar c = Calendar.getInstance();        // This is the right way to set the month    c.set(Calendar.MONTH, Calendar.FEBRUARY);    c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));    System.out.println(c.getTime());    // You probably do this:    c.set(Calendar.MONTH, 2); // SO wrong!!! Same as c.set(Calendar.MONTH, Calendar.MARCH);    c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));    System.out.println(c.getTime());
 
This topic has 5 replies on 1 page.