Java Get First Date of Current Year
In this Java tutorial, we learn how to get the first day of current year as a Date object with the time set to the start of the day.
How to get first day of current year in Java
In this first step, create a new Java class named DateUtil, and implement a static method named getFirstDayOfCurrentYear() to return the first Date of current year as following Java code.
DateUtil.java
import java.util.Calendar;
import java.util.Date;
public class DateUtil {
/**
* Get first day of current year
* @return return first day of current year as Date object
*/
public static Date getFirstDayOfCurrentYear() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_YEAR, calendar.getActualMinimum(Calendar.DAY_OF_YEAR));
calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMinimum(Calendar.HOUR_OF_DAY));
calendar.set(Calendar.MINUTE, calendar.getActualMinimum(Calendar.MINUTE));
calendar.set(Calendar.SECOND, calendar.getActualMinimum(Calendar.SECOND));
calendar.set(Calendar.MILLISECOND, calendar.getActualMinimum(Calendar.MILLISECOND));
return calendar.getTime();
}
}
In the following example Java program, we learn how to use the above DateUtill class to get the first Date of current year.
GetFirstDayOfYearExample.java
import java.util.Date;
public class GetFirstDayOfYearExample {
public static void main(String... args) {
// Get First Date of Current Year
Date firstDayOfYear = DateUtil.getFirstDayOfCurrentYear();
System.out.println("Today: " + new Date());
System.out.println("First day of current year: " + firstDayOfYear);
}
}
Today: Tue Aug 16 21:01:18 ICT 2022
First day of current year: Sat Jan 01 00:00:00 ICT 2022
Happy Coding 😊
Related Articles
Java Convert Date to Start of Day Time
Java Convert Date to End of Day Time
Java Get First Date of Current Month
Java Get Same Date in Last Month
Java Get First Day of Month from Specified Date
Java Get Last Date of Current Month
Java Get Last Date of Current Year
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 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