How do I get the last day of a month?

Category: java.util, viewed: 34224 time(s).
package org.kodejava.example.util;

import java.util.Calendar;

public class LastDayOfMonth {
    public static void main(String[] args) {
	//
	// Get a calendar instance
	//
	Calendar calendar = Calendar.getInstance();
	
	//
	// Get the last date of the current month. To get the last date for a
	// specific month you can set the calendar month using calendar object
	// calendar.set(Calendar.MONTH, theMonth) method.
	//	
	int lastDate = calendar.getActualMaximum(Calendar.DATE);
	
	//
	// Set the calendar date to the last date of the month so then we can
	// get the last day of the month
	//
	calendar.set(Calendar.DATE, lastDate);	
	int lastDay = calendar.get(Calendar.DAY_OF_WEEK);
	
	//
	// Print the current date and the last date of the month
	//
	System.out.println("Last Date: " + calendar.getTime());
	
	//
	// The lastDay will be in a value from 1 to 7 where 1 = monday and 7 =
	// saturday respectively.
	//
	System.out.println("Last Day : " + lastDay);	
    }
}
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Uncensored Newsgroups
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Java Training

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats