Getting the current year in Java is a common task that is used to perform different operations like data analysis, data filtering, etc. For example, it helps us calculate the employee ages, filter the data based on the current year, students appear in an exam in the current year, etc. To do this task in Java, different built-in and third-party classes/libraries are used that will be discussed in this post with examples.
How to Get Current Year in Java
You can use one of the below-listed classes to get the current year in Java:
- Using Date Class
- Using Calendar Class
- Using LocalDateTime Class
- Using Year Class
- Using Instant and ZoneId
- Using LocalDate Class
- Using YearMonth Class
- Using OffsetDateTime Class
- Using CalendarUtils Class
- Using the Joda Time Package
1. Using Date Class
Before the release of Java 8, the Date class was one of the most convenient ways to get the current year in Java. But now the stated class is depreciated from Java 8 and later versions. However, it’s still valuable and is often used when developers need to maintain the legacy code. To get the current year using the Date class, you can use the “getYear()” method as follows:
import java.util.Date;
public class GetCurrentYearJava {
public static void main(String[] args) {
Date getCurrentDate = new Date();
int getCurrentYear = getCurrentDate.getYear() + 1900;
System.out.println("Getting Current Year Using Date Class ⇒ " + getCurrentYear);
}
}
In this example,
- First, we import the Date class from the “java.util” package.
- In the main() method, we create an instance of the “Date” class “getCurrentDate” to get the current date.
- In the next line, we invoke the getYear() method of the Date class to get the current year from the current date.
- The getYear() retrieves an integer indicating the total years since 1900. Therefore to get the appropriate current date we add 1900 in the extracted year.
- Finally, we print the obtained current year on the console:
Pro Tip: You can use the Date class with the SimpleDateFormat class to get the current year in a specific format. For example, in the below, code we use the SimpleDateFormat class to get the current year in “yy” format (last two digits of the year):
import java.text.SimpleDateFormat;
import java.util.Date;
public class GetCurrentYearJava {
public static void main(String[] args) {
Date currDate = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat();
dateFormat.applyPattern("yy");
String currYear = dateFormat.format(currDate);
System.out.println("Current Year Using SimpleDateFormat Class ⇒ " +currYear);
}
}
We create an instance of the SimpleDateFormat class and invoke the applyPattern() method on it to set the specified pattern to the date format. After this, we call the format() method on the current date to format it according to the specified pattern. Finally, we display the formatted current year on the console:
2. Using Calendar Class
Java offers an abstract class named “Calendar” that lets us convert dates between specific instant and calendar fields like a year. It offers a get() method that is invoked with the “Calendar.Year” as its argument to get/retrieve the current year.
Here it is important to note that you can’t use the constructor of the Calendar class to create its instance (since it is an abstract class). Therefore, in the following code, first, we import the Calendar class and then get an instance of this class using the getInstance() method.
import java.util.Calendar;
public class GetCurrentYearJava {
public static void main(String[] args) {
Calendar getCurrentDate = Calendar.getInstance();
int getCurrentYear = getCurrentDate.get(Calendar.YEAR);
System.out.println("Getting Current Year Using Calendar Class ⇒ " + getCurrentYear);
}
}
The getInstance() method retrieves the current date and time. So, we invoke the “get(Calendar.YEAR)” method on the retrieved date and time to extract the current year:
Pro Tip: We can use the “GregorianCalendar” (a subclass of Calendar) to get the current year or a specific year from a date:
import java.util.GregorianCalendar;
public class GetCurrentYearJava {
public static void main(String[] args) {
GregorianCalendar currDate = new GregorianCalendar();
int currYear = currDate.get(GregorianCalendar.YEAR);
System.out.println("Getting Current Year Using GregorianCalendar Class ⇒ " + currYear);
GregorianCalendar specificDate = new GregorianCalendar(2012, 06, 01);
int specificYear = specificDate.get(GregorianCalendar.YEAR);
System.out.println("Getting Specific Year Using GregorianCalendar Class ⇒ " + specificYear);
}
}
From the output, you can observe that the get() method extracts and retrieves the year field from the current and specified dates:
3. Using LocalDateTime Class
The “LocalDateTime” is an immutable DateTime Object in Java that represents a DateTime in the default format as “year-month-day-hour-minute-second”. You can invoke the now() method using this object to retrieve the current Date and Time. After this, you can use the getYear() method of this class to extract the current year from the current DateTime:
import java.time.*;
public class GetCurrentYearJava {
public static void main(String[] args) {
LocalDateTime currDate = LocalDateTime.now();
System.out.println("The Current Date is ⇒ " + currDate);
System.out.println("Getting Current Year Using LocalDateTime Class ⇒ " + currDate.getYear());
}
}
The output shows the current DateTime and the current year using the LocalDateTimeClass:
4. Using Year Class
The Year is a built-in class of the “java.time” package that represents a year in ISO 8601 format. It offers a static method named now() that retrieves the current year. The following code uses this method to retrieve the current year on the console:
import java.time.*;
public class GetCurrentYearJava {
public static void main(String[] args) {
Year currDate = Year.now();
System.out.println("The Current Year Using Year Class ⇒ " + currDate);
}
}
The “Year.now()” method successfully retrieves the current year, as demonstrated below:
Alternative Approaches
The below table shows a couple of alternative approaches to get the current year using the Year class:
Method Name | Description | How to Use |
---|---|---|
getValue() | Use this method with the “Year.now()” method to get the current year in integer format. | Year.now().getValue(); |
systemUTC() | This method belongs to the clock class and is used to get/retrieve the current DateTime. Wrap this method within the “now()” method of the year class to extract the current year. | Year.now(Clock.systemUTC); |
systemDefault() | This method is available in the ZoneId class and is used to get the zone ID. You can use this method with the help of Year class to get the current year. | Year.now(ZoneId.systemDefault()); |
5. Using Instant and ZoneId
The Instant class helps us get/retrieve the current date time without the time zone and offset information. However, extracting a date from a timeline requires information like zone ID. For this purpose, we can use classes like ZoneId and ZonedDateTime to get the zone ID and offset in ISO 8601 format. After this, we can use the getYear() method to extract the current year:
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class GetCurrentYearJava {
public static void main(String[] args) {
Instant currInstant = Instant.now();
ZonedDateTime zonedDate = currInstant.atZone(ZoneId.systemDefault());
int currYear = zonedDate.getYear();
System.out.println("Current Year Using Instant and ZoneId Classes ⇒ " + currYear);
}
}
In the above code, we use “Instant.now()” to get the current instant. Also, we invoke the atZone() method on the current instant and pass the “ZoneId.systemDefault()” method as its argument. This will return the zoned date time. Finally, we invoke the getYear() method on the zonedDate to get the current year from it:
Pro tip: You can also get the current year directly using the “ZonedDateTime.now().getYear();”.
6. Using LocalDate Class
The “LocalDate” is an immutable Date Object in Java that represents a date in the default “year-month-day” format. It offers a built-in now() method that retrieves the current date. You can invoke the getYear() method on the retrieved current date to extract the current year from it:
import java.time.*;
public class GetCurrentYearJava {
public static void main(String[] args) {
LocalDate currDate = LocalDate.now();
int year = currDate.getYear();
System.out.println("The Current Year Using LocalDate Class ⇒ " + year);
}
}
The output below shows the current year extracted from the local date:
7. Using YearMonth Class
The “YearMonth” class of the “java.time” package offers a getYear() method that can be used with the now() method to get a current year. The below code prints the current year on the console using the YearMonth class:
import java.time.YearMonth;
public class GetCurrentYearJava {
public static void main(String[] args)
{
System.out.println("Current Year Using YearMonth Class ⇒ " + YearMonth.now().getYear());
}
}
On successful execution of the YearMonth class, the current year will appear on the console as follows:
8. Using OffsetDateTime Class
The “OffsetDateTime” class of the “java.time” package offers a getYear() method that extracts a year from a date. This class is usually used to deal with applications that communicate with a network protocol or database.
Let’s create an object of the OffsetDateTime class and call the now() method to get/retrieve the current date time:
import java.time.OffsetDateTime;
public class GetCurrentYearJava {
public static void main(String[] args) {
OffsetDateTime currDateTime = OffsetDateTime.now();
int currYear = currDateTime.getYear();
System.out.println("Current Year Using OffsetDateTime Class ⇒ " + currYear);
}
}
We invoke the “getYear()” method on the currDateTime object to get the current year from it:
9. Using CalendarUtils Class
A third-party library “Apache Commons” offers a CalendarUtils class that is designed to work with the DateTime values. To use this class, you must add the latest version of the Apache Commons lang dependency into the “pom.xml” file of your Maven project:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.14.0</version>
</dependency>
Now, import the corresponding dependency at the start of your “java” file and write the below code to get the current year:
import org.apache.commons.lang3.time.CalendarUtils;
public class GetCurrentYearJava {
public static void main(String[] args) {
CalendarUtils currDate = CalendarUtils.INSTANCE;
System.out.println("Getting Current Year Using CalendarUtilsClass => " + currDate.getYear());
}
}
In this code, first, we create an instance of the CalendarUtils class. After this, we call the getYear() method on the “currDate” object to extract/get the current year from it:
10. Using the Joda Time Package
The “Joda Time” package offers several classes like “DateTime” to deal with the Date and Time values. Java developers use this package when they need excessive use of date and calendar objects in their application. Also, it is frequently used in legacy codebases (where Java 8 or later features are unavailable). To use this package, add the below-given dependency into your maven project:
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.12.7</version>
</dependency>
After this use the following “import” statement to add/import the DateTime Class from the “joda.time” package:
import org.joda.time.DateTime;
public class GetCurrentYearJava {
public static void main(String[] args) {
DateTime currDateTime = new DateTime();
int currYear = currDateTime.getYear();
System.out.println("Getting Current Year Using Joda Package => " + currYear);
}
}
In the main() method, first, we create an object of the DateTime class to get the current date and time. After this, we invoke the getYear() method to get/extract the current year from the current DateTime. Finally, we print the extracted current year on the console:
This is how you can get the current year in Java.
Conclusion
To get the current year in Java, you can use different built-in classes as well as third-party libraries, such as Calendar, Year, CalendarUtils, Joda Time, etc. Using all these classes, you can get the current year, however, all of them have different use cases. For example, some of them are suitable for legacy codebases, some are suitable for users who can’t access Java 8 or the latest features, and some are useful for communicating with databases and network protocols.