Java Get First LocalDate of Current Year
Tags: Java LocalDate DateUtil
In this Java tutorial, we learn how to get the first day of current year and return it as a LocalDate object in Java programming language.
How to get first LocalDate of current year in Java
At this first step, we create a new Java method named DateUtil, and implement a new static method named getFirstLocalDateOfCurrentYear() with the following steps to get first day of current year.
- Step 1: get the value of today as LocalDate using the LocalDate.now() method.
- Step 2: get the first day of current year based on today value using the LocalDate.withDayOfYear(int dayOfYear) method.
DateUtil.java
import java.time.LocalDate;
public class DateUtil {
/**
* This method to get the first day of current year and return it as a LocalDate object.
* @return the first LocalDate of current year.
*/
public static LocalDate getFirstLocalDateOfCurrentYear() {
LocalDate today = LocalDate.now();
LocalDate firstDayOfYear = today.withDayOfYear(1);
return firstDayOfYear;
}
}
In the following example Java code, we learn how to use the above DateUtil class to get the first day of current year as a LocalDate object in Java program.
GetFirstLocalDateOfCurrentYearExample.java
import java.time.LocalDate;
public class GetFirstLocalDateOfCurrentYearExample {
public static void main(String... args) {
// Get First LocalDate of Current Year
LocalDate firstDayOfYear = DateUtil.getFirstLocalDateOfCurrentYear();
System.out.println("Today: " + LocalDate.now());
System.out.println("First day of current year: " + firstDayOfYear);
}
}
Today: 2022-08-20
First day of current year: 2022-01-01
Happy Coding 😊
Related Articles
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 Last 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