Get last day of current month in java
This article will discuss various ways to calculate last day of current month in java using below approaches.
2. Java 8 TemporalAdjusters
3. Java Calendar class
4. Apache Commons Lang library
5. Joda time library
Java 8 introduced a new class LocalDate for date handling.
It contains methods to add, subtract days, months, years from a date.
Besides, it has a
now()
method to get the current date.
Secondly, there is an YearMonth
class, which is a combination of year and month of a date.
It has a lengthOfMonth()
method, which returns the number of days in a month for that year.
To create an object of YearMonth
, use its static of()
method.
So, the combination of LocalDate
and YearMonth
classes can be used to get last day of current month.
import java.time.LocalDate; import java.time.YearMonth; public class LastDayOfMonthExample { public static void main(String[] args) { // get the current date LocalDate currentDate = LocalDate.now(); // get year and month from the current date int year = currentDate.getYear(); int month = currentDate.getMonthValue(); // create a YearMonth object for the current year and month YearMonth yearMonth = YearMonth.of(year, month); // get last day of the current month as a day number int lastDayOfMonth = yearMonth.lengthOfMonth(); System.out.println("Last day of the month: " + lastDayOfMonth); } }
This prints
Last date of the month: 31
Java 8 introduced another class TemporalAdjusters
, which contains static methods to get date components such as finding first or last day of month, year etc.
LocalDate
has a with()
method that access TemporalAdjusters
argument and returns a new date adjusted according to the object that is supplied to it.
So, we supply a TemporalAdjusters
object to get last day of month using its lastDayOfMonth()
method to with()
method of LocalDate
.
On the new date that is returned by with(), we call getDayOfMonth()
, which will return the last of current month as shown below.
import java.time.LocalDate; import java.time.temporal.TemporalAdjusters; public class LastDayOfMonth { public static void main(String[] args) { // get the current date LocalDate currentDate = LocalDate.now(); // get last day of current month using TemporalAdjusters LocalDate lastDay = currentDate. with(TemporalAdjusters.lastDayOfMonth()). getDayOfMonth(); System.out.println("Last day of the month: " + lastDay); } }
java.util.Calendar
class has get()
methods to get components of a date, set()
and add()
methods to change date fields.
Using these methods, we move the current date to the first day of next month and then subtract 1 from it.
This will move the date to the last day of current month and retrieve the last day using its get()
method as illustrated in below example.
import java.util.Calendar; public class LastDayOfMonthExample { public static void main(String[] args) { // get the current date Calendar calendar = Calendar.getInstance(); // get its year and month int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); // get first day of the next month, then go back one day // and get the last day of the current month calendar.set(year, month + 1, 1); calendar.add(Calendar.DATE, -1); // get the last day of the current month as a day number int lastDayOfMonth = calendar.get(Calendar.DAY_OF_MONTH); System.out.println("Last day of the month: " + lastDayOfMonth); } }
4. Using DateUtils
This method also works on the same logic as previous method. That is, first we move the current date to first day of next month and then subtract 1 day from it.
The result will be the last date of previous month(or current month initially)
Apache Commons Lang library has a DateUtils
class having method truncate()
, which sets the time of the date object to the beginning of the month.
This means the time components (hours, minutes, seconds, milliseconds) will be set to zero for the current date.
Then, using its addMonths()
method, add one month to the truncated date. By doing this, we get the first day of the next month.
Thirdly, use its addDays()
method, we subtract one day from the first day of the next month, which gives the last day of the current month.
Finally, convert the date object from last step to java.util.Calendar
with toCalendar()
method and get the last day from date object using its get()
method.
import org.apache.commons.lang3.time.DateUtils; public class LastDayOfMonthExample { public static void main(String[] args) { // get the current date Date currentDate = new Date(); Date lastDay = DateUtils.addMonths( DateUtils. truncate(currentDate, Calendar.MONTH), 1 ); lastDay = DateUtils.addDays(lastDay, -1); // get the last day of the current month as a day number int lastDayOfMonth = DateUtils.toCalendar(lastDay). get(Calendar.DAY_OF_MONTH); System.out.println("Last day of the month: " + lastDayOfMonth); } }
To include this library in your project, add below dependencies as per the build tool.
// GRADLE implementation 'org.apache.commons:commons-lang3:3.12.0' <!-- MAVEN --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.12.0</version> </dependency>
5. Using joda time library
Joda time library has a class DateTime
, having now()
method, which is used to get current date.
It also has dayOfMonth()
and withMaximumValue()
methods, which return the max day of a month, when used together.
Maximum day of a month is obviously the last day of month.
import org.joda.time.DateTime; import org.joda.time.DateTimeConstants; public class LastDayOfMonthExample { public static void main(String[] args) { // get the current date DateTime currentDate = DateTime.now(); // get the last day of the month DateTime lastDayOfMonth = currentDate. dayOfMonth(). withMaximumValue(); System.out.println("Last day of the month: " + lastDayOfMonth); } }
To include this library in your project, add below dependencies as per the build tool.
// GRADLE implementation 'joda-time:joda-time:2.12.5' <!-- MAVEN --> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.12.5</version> </dependency>
Hope the article was useful.