Get Last Date of Current Month using Calendar

Tags: Date and Time

Java Code Examples for java.util.Calendar

Get Last Date of Current Month using Calendar

package simplesolution.dev;

import java.util.Calendar;
import java.util.Date;

public class CalendarLastDateOfMonthExamples {

    public static void main(String... args) {
        Calendar calendar = Calendar.getInstance();
        Date currentDate = new Date();
        calendar.setTime(currentDate);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.MILLISECOND, -1);

        Date lastDateOfCurrentMonth = calendar.getTime();

        System.out.println(lastDateOfCurrentMonth);
    }
}

Happy Coding 😊