Java calendar last day of month.

Java calendar last day of month.


Posted in : Core Java Posted on : December 1, 2010 at 4:21 PM Comments : [ 0 ]

Java calendar last day of month.

Java calendar last day of month.

In this section, you will see how to access last day of month.

In this example, first of all we have to creates a instance of Calendar class. Which holds the instance of a date. Here, we are using a getActualMaximum(int field) method of Calendar class in java. It returns maximum value of field. Here, it is returning maximum date of month. After this we are using set(int field, int value) method. It resets the calendar date field at maximum date of month(last date),  and the get(Calendar.DAY_OF_WEEK) returns last day of week.

Code:

LastDayOfMonth.java
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class LastDayOfMonth {
public static void main(String[] args) {
	Calendar calendarDate = Calendar.getInstance();
	int lastDate = calendarDate.getActualMaximum(Calendar.DATE);
	System.out.println("Last date of current month : " + lastDate);
	calendarDate.set(Calendar.DATE, lastDate);
	int lastDay = calendarDate.get(Calendar.DAY_OF_WEEK);
	System.out.println("Lastday of month : " + lastDay);
	String strPattern = "EEEE";
	SimpleDateFormat obDateFormat = new SimpleDateFormat(strPattern);
	String nameOfLastDay = obDateFormat.format(calendarDate.getTime());
	System.out.println("Name of last day of month : " + nameOfLastDay);
    }
}

Output

Last date of current month : 31
Lastday of month : 6
Name of last day of month : Friday

Download this code.

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics