Code Snippets: Java Calendar – How to get the last day of the Month

The Java Calendar class provides many helpful methods for modifying Date objects. Here you will find a short code snippet for obtaining the last day of the month. To utilize the code please don’t forget to import the java.util.Calendar class or which ever Calendar you utilize. How the snippet works is it utilizes the getActualMaximum function which returns the max value for a field based on the current settings of the Calendar.

Calendar cal = Calendar.getInstance();
cal.set( Calendar.MONTH, 11 );
cal.set( Calendar.DATE, cal.getActualMaximum(Calendar.DAY_OF_MONTH) );
// Code Snippets // Java //

Comments & Questions

Add Your Comment