Java Get Last LocalDate of Current Year

Tags: Java LocalDate DateUtil

In this Java tutorial, we learn how to get the last day of current year and return it as a LocalDate object in Java programming language.

How to get last LocalDate of current year in Java

In this first step, we create a new Java class named DateUtil, and implement a new static method named getLastLocalDateOfCurrentYear() to populate the last day of current year and return it as a LocalDate object as the following Java code.

DateUtil.java

import java.time.LocalDate;

public class DateUtil {

    /**
     * This method to get the last day of current year and return it as a LocalDate object.
     * @return the last LocalDate object of current year.
     */
    public static LocalDate getLastLocalDateOfCurrentYear() {
        LocalDate today = LocalDate.now();
        LocalDate lastDayOfYear = today.plusYears(1).withDayOfYear(1).minusDays(1);
        return lastDayOfYear;
    }
    
}

In the following example Java code, we learn how to use the getLastLocalDateOfCurrentYear() static method above to get last day of current year as LocalDate object in Java program.

GetLastLocalDateOfCurrentYearExample.java

import java.time.LocalDate;

public class GetLastLocalDateOfCurrentYearExample {
    public static void main(String... args) {
        // Get Last LocalDate of Current Year
        LocalDate lastDayOfYear = DateUtil.getLastLocalDateOfCurrentYear();

        System.out.println("Today: " + LocalDate.now());
        System.out.println("Last day of current year: " + lastDayOfYear);
    }
}
The output as below.
Today: 2022-08-20
Last day of current year: 2022-12-31

Happy Coding 😊

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

Java Get First LocalDate of Current Year

Java Get First Day of Month from Specified Date

Java Get Last Date of Current Year

Java Get Last Date of Specified Month

Java Get Last Date of Specified Year

Java Get First Date of Current Year

Java Get First Date of Current Month

Java Get Same Date in Last Month