You want to know what is the last date for a current month. The code below show you how to get it.
package org.kodejava.util;
import java.util.Calendar;
public class LastDateOfMonth {
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);
// Print the current date and the last date of the month
System.out.println("Date : " + calendar.getTime());
System.out.println("Last Date: " + lastDate);
}
}
Here is the result of the code snippet:
Date : Fri Sep 24 06:42:18 CST 2021
Last Date: 30
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024