Javatpoint Logo
Javatpoint Logo

Get Year from Date in Java

In Java, working with dates is a common task, especially when dealing with applications that require tracking dates and times. One of the most common operations involving dates is getting the year from a given date. Fortunately, Java provides several ways to do this, and one of them is the getYear() method.

The getYear() method is a part of the java.util.Date class in Java, which represents a specific point in time. It returns the year of a date represented by the Date object, counting from 1900. For example, if the date represented by the Date object is "January 1, 2022," then the getYear() method will return 122 (i.e., 2022 - 1900 = 122).

Here is an example code snippet that demonstrates how to use the getYear() method to get the year from a given date:

GetYearExample.java

Output:

Current Year: 2023

In this example, we first create a new Date object, which represents the current date and time. We then use the getYear() method to get the year from the Date object and add 1900 to it to get the actual year. Finally, we print the current year to the console.

We have the getYear() method is deprecated since Java 1.1 and is replaced by getYear() method in the java.util.Calendar class. Here's an example of how to use Calendar.get(Calendar.YEAR) to get the year from a given date:

GetYearExample.java

Output:

Current Year: 2023

In this example, we create a Calendar object using the getInstance() method, which represents the current date and time. We then use the get() method with the Calendar.YEAR field to get the year from the Calendar object. Finally, we print the current year to the console.

Getting the year from a date in Java is a simple task, thanks to the getYear() method in the java.util.Date class or the get(Calendar.YEAR) method in the java.util.Calendar class. By using these methods, you can easily retrieve the year from a date and perform any necessary operations with it. The getYear() method in the java.util.Date class is deprecated and should not be used. Instead, the recommended approach is to use the get(Calendar.YEAR) method in the java.util.Calendar class or the getYear() method in the java.time.LocalDate class, which was introduced in Java 8 as part of the new Date and Time API.

Here's an example of how to use the get(Calendar.YEAR) method to get the year from a specific date:

GetYearExample.java

Output:

Year: 2022

In this example, we create a Calendar object using the getInstance() method, which represents the current date and time. We then set the Calendar object to January 1, 2022, using the set() method and the Calendar.JANUARY constant for the month. We then use the get(Calendar.YEAR) method to get the year from the Calendar object and print it to the console.

Here's an example of how to use the getYear() method in the java.time.LocalDate class to get the year from a specific date:

GetYearExample.java

Output:

Year: 2022

In this example, we have created a LocalDate object that represents January 1, 2022, using the of() method. We then use the getYear() method to get the year from the LocalDate object and print it to the console.

Getting the year from a date in Java is a simple task, and there are several ways to do it, depending on the version of Java you're using and the specific requirements of your application. By using the appropriate method for your situation, you can easily extract the year from a date and perform any necessary operations with it.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA