Java Get Last Date of Current Year
In this Java tutorial, we learn how to write Java program to get the last day of current year and return it as a Date object in Java programming language.
How to get last day of current year in Java
In this first step, we create a new Java class named DateUtil, and implement a new static method named getLastDateOfCurrentYear() which return the last day of current year as a Date object with the time value set to the end of the day.
DateUtil.java
import java.util.Calendar;
import java.util.Date;
public class DateUtil {
/**
* This method to get the last day of current year and return as a Date object
* @return the last Date of current year object
*/
public static Date getLastDateOfCurrentYear() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_YEAR, calendar.getActualMaximum(Calendar.DAY_OF_YEAR));
calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));
return calendar.getTime();
}
}
In the following example Java code, we learn how to write Java program to use the DateUtil class above to get the last Date of the current year value.
GetLastDateOfCurrentYearExample.java
import java.util.Date;
public class GetLastDateOfCurrentYearExample {
public static void main(String... args) {
// Get Last Date of Current Year
Date lastDateOfYear = DateUtil.getLastDateOfCurrentYear();
System.out.println("Today: " + new Date());
System.out.println("Last Date of current year: " + lastDateOfYear);
}
}
Today: Wed Aug 17 21:17:17 ICT 2022
Last Date of current year: Sat Dec 31 23:59:59 ICT 2022
Happy Coding 😊
Related Articles
Java Get First Day of Month from Specified Date
Java Get Last Date of Current Month
Java Get Last Date of Specified Month
Java Get Last Date of Specified Year
Java Check if Calendar is Week Day or Weekend Day
Java Check if Date is Week Day or Weekend Day
Java Check if Today is Week Day or Weekend Day
Java Convert Date to Start of Day Time
Java Convert Date to End of Day Time
Java Get First Date of Current Year
Java Get First Date of Current Month
Java Get Same Date in Last Month
Java Get First Date of Current Week
Java Get Last Date of Current Week
Java Get First LocalDate of Current Week
Java Get Last LocalDate of Current Week
Java Get First LocalDate of Current Month
Java Get Last LocalDate of Current Month