Get last day of Month in java

Get last day of month in java

Learn about how to get last day of month in java.
There are times when you need to get last day of month to process data at the end of each month.
For example: Processing salaries of the employee in your company.

There are multiple ways to get last day of month in java

Ways to get last day of month in java

Using LocalDate to get last day of month in Java 8

Here are the steps to get last day of month using LocalDate

  • Create new LocalDate object either by using LocalDate.now() or using given date.
  • Get Month object by calling LocalDate's getMonth() method.
  • Use Month's length() method get number of days in the month.
  • Finally, use LocalDate's WithDayOfMonth() method to get LocalDate from given day of the month.
package org.arpit.java2blog;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class GetLastDayOfMonthLocalDate {

    public static void main(String[] args) {

        // Get last day of month for current date
        LocalDate currentDate = LocalDate.now();
        currentDate.getMonth();
        LocalDate lastDayOfMonthDate  = currentDate.withDayOfMonth(
                                        currentDate.getMonth().length(currentDate.isLeapYear()));
        System.out.println("Last date of the month: "+lastDayOfMonthDate);

        // Get last day of month for given date
        String date = "13/01/2022";
        LocalDate givenDate = LocalDate.parse(date, DateTimeFormatter.ofPattern("dd/MM/yyyy"));
        LocalDate lastDayOfMonthDateGivenDate  = givenDate.withDayOfMonth(
                                                 givenDate.getMonth().length(givenDate.isLeapYear()));

        System.out.println("Last date of the month for date 13/01/2022: "+lastDayOfMonthDateGivenDate);
    }
}

Output:

Last date of the month: 2022-02-28
Last date of the month for date 13/01/2022: 2022-01-31

Using Calendar to get last day of month in java

Here are the steps to get last day of month using Calendar class.

  • Get Calendar obj using Calendar.getInstance()
  • Get lastDayOfMonth using Calendar's getActualMaximum() based on Calendar.Date
  • Set lastDateOfMonth to Calendar.Date
  • Extract all relevant informations such Last day of month or Day of week from Calendar object.
package org.arpit.java2blog;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class GetLastDayOfMonthCalendar {
    public static void main(String[] args) throws ParseException {
        Calendar cal = Calendar.getInstance();
        int lastDateOfMonth = cal.getActualMaximum(Calendar.DATE);

        cal.set(Calendar.DATE, lastDateOfMonth);

        // Get last date of current month
        Date lastDateOfMonthDate = cal.getTime();
        System.out.println("Last date of month: "+cal.getTime());
        System.out.println("Last day of month: "+lastDateOfMonth);

        int lastDayOfWeekNumber = cal.get(Calendar.DAY_OF_WEEK);
        System.out.println("Day of week in Number: "+lastDayOfWeekNumber);

        String dayWeekText = new SimpleDateFormat("EEEE").format(lastDateOfMonthDate);
        System.out.println("Day of week in text:"+dayWeekText);

        System.out.println("======================================");

        // Last day of month for any given date
        String date = "13/01/2012";
        SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        Date givenDate = df.parse(date);
        cal.setTime(givenDate);

        int lastDateOfMonthForGivenDate = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DATE, lastDateOfMonthForGivenDate);

        System.out.println("Last date of month: "+cal.getTime());
        System.out.println("Last day of month: "+lastDateOfMonthForGivenDate);

        int lastDayOfWeekNumberForGivenDate = cal.get(Calendar.DAY_OF_WEEK);

        System.out.println("Day of week in Number: "+lastDayOfWeekNumberForGivenDate);

        String dayWeekTextForGivenDay = new SimpleDateFormat("EEEE").format(lastDayOfWeekNumberForGivenDate);
        System.out.println("Day of week in text:"+dayWeekTextForGivenDay);
    }
}

Output:

Last date of month: Mon Feb 28 18:43:05 IST 2022
Last day of month: 28
Day of week in Number: 2
Day of week in text:Monday
======================================
Last date of month: Tue Jan 31 00:00:00 IST 2012
Last day of month: 31
Day of week in Number: 3
Day of week in text:Thursday

Get last day of previous month in java

You need to subtract -1 from Calendar.Month and use getActualMaximum() to get last of previous month in java.

   // Substracting one month from Calendar
   cal.add(Calendar.MONTH, -1);

Here is complete example

package org.arpit.java2blog;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class GetLastDayOfMonthPreviousMonth {
    public static void main(String[] args) throws ParseException {
        Calendar cal = Calendar.getInstance();
        String date = "13/01/2022";
        SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        Date givenDate = df.parse(date);
        cal.setTime(givenDate);

        // Substracting one month from Calendar
        cal.add(Calendar.MONTH, -1);
        int lastDateOfPreviousMonth = cal.getActualMaximum(Calendar.DATE);

        // Get last date of previous month
        cal.set(Calendar.DATE, lastDateOfPreviousMonth);

        Date lastDateOfPreviousMonthDate = cal.getTime();
        System.out.println("Last date of previous month: "+lastDateOfPreviousMonthDate);

    }
}

Output:

Last date of previous month: Fri Dec 31 00:00:00 IST 2021

Get last day of next month in java

You need to add 1 from Calendar.Month and use getActualMaximum() to get last of next month in java.

  // Adding one month to Calendar
  cal.add(Calendar.MONTH, 1);

Here is complete example

package org.arpit.java2blog;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class GetLastDayOfMonthNextMonth {
    public static void main(String[] args) throws ParseException {
        Calendar cal = Calendar.getInstance();

        String date = "13/01/2022";
        SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        Date givenDate = df.parse(date);
        cal.setTime(givenDate);

        // Adding one month to Calendar
        cal.add(Calendar.MONTH, 1);
        int lastDateOfNextMonth = cal.getActualMaximum(Calendar.DATE);

        // Get last date of next month
        cal.set(Calendar.DATE, lastDateOfNextMonth);

        Date lastDateOfNextMonthDate = cal.getTime();
        System.out.println("Last date of next month: "+lastDateOfNextMonthDate);

    }
}

Output:

Last date of next month: Mon Feb 28 00:00:00 IST 2022

That’s all about how to get last day of month in java.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.