Table of Contents
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 callingLocalDate's getMonth()
method. - Use
Month's length()
method get number of days in the month. - Finally, use
LocalDate's WithDayOfMonth()
method to getLocalDate
from given day of the month.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
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 for date 13/01/2022: 2022-01-31
Further reading:
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
usingCalendar's getActualMaximum()
based onCalendar.Date
- Set lastDateOfMonth to
Calendar.Date
- Extract all relevant informations such Last day of month or Day of week from Calendar object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
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 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.
1 2 3 4 |
// Substracting one month from Calendar cal.add(Calendar.MONTH, -1); |
Here is complete example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
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:
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.
1 2 3 4 |
// Adding one month to Calendar cal.add(Calendar.MONTH, 1); |
Here is complete example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
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:
That’s all about how to get last day of month in java.