Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Java-get-example Get Last Day of The Month

Related Tutorials


 
 

Share on Google+Share on Google+

Get Last Day of The Month

Advertisement
Calendar class inherits from Java.util.calendar.Calendar is base abstract class, that is used for converting between a Date object and a set of integer fields such as YEAR, MONTH, DAY, HOUR.

Get Last Day of The Month

     

Calendar class inherits from Java.util.calendar.Calendar is  base abstract class, that is used for converting between a Date object and a set of integer fields such as YEAR, MONTH, DAY, HOUR.. A Date object  specific instant in time with millisecond.

In this Tutorial we want to describe you a code that helps you in getting the last day of the month. For this we have a class name MonthDaysExample.Inside the main method the calendar call the get Instance method  ( ) returns you the calendar object with a specific time zone and locale. we declared and  initialized a int variable year, month and date.

1)calendar.set( )  -This method set the field for year, month and date.

2)getActualMaximum( )- This method  Return the maximum value for this field, given the current date and store its value int variable max day.

The println print the Max Day  from the int variable max day of max day of month February 2009.

In the same way we print the Max day of  February 2004 in this code.

MonthDaysExample.java

import java.text.*;
import java.util.*;

public class MonthDaysExample {

  public static void main(String args[]) {

  Calendar calendar = Calendar.getInstance();

  int year = 2009;
  int month = Calendar.FEBRUARY;
  int date = 1;

  calendar.set(year, month, date);

  int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
  System.out.println("Max Day: " + maxDay);

  calendar.set(2004, Calendar.FEBRUARY, 1);
  maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
  System.out.println("Max Day: " + maxDay);

  }
}

Output:

Max Day: 28

Max Day: 29


Download code
Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 6, 2008

Related Tutorials

Discuss: Get Last Day of The Month  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:2
Scott
December 16, 2011
Thanks

Thanks a lot, this was a lot of help to me
Tsuru
April 17, 2012
:)

Just what I was looking for! Thanks for the post
DMCA.com