Getting the year from a date in Java is a simple concept that involves different methods. A date is an important object in tasks that we perform daily. From scheduling a meeting to booking an appointment, dates play an important role. Therefore, Java offers numerous methods for working with dates. More specifically, this article will involve methods to get the year from the date in Java.
How to Get/Fetch Year from Date in Java?
Various methods and functions are available in Java programming that provide a way to get the year from the date in Java. These methods provide an effective way to get the year. Several approaches are present to get the desired year from the provided date. These approaches in Java are listed below:
- Calendar class
- Local Date class
- String.split() method
- SimpleDateFormat class
Algorithm
The algorithm to get the year from the date is depicted below:
- Create a main class in Java with the method to get the year.
- Use built-in functions available in Java.
- Print the year from the entered date.
Method 1: Using Calendar Class
The below example uses one of the built-in methods of the calendar class to get the year from the provided date:
import java.util.*;
class getyear {
public static void main(String args[])
{
//date is entered according to year, month, and the current date
Calendar newcal= new GregorianCalendar(2023,8,18);
// The year from the date is extracted
int year = newcal.get(Calendar.YEAR);
// The output is printed on the screen
System.out.println("The extracted Year from the date is: " + year);
}
}
The GregorianCalendar() is a built-in that is used in the above code to get the year, date, and month. As a result, it fetches the year from the date.
Output

In the above output, the year from the entered date has been obtained.
Method 2: Using Local Date Class
This example utilizes the built-in method named LocaDate() to obtain the year from the specified date.
//import local date
import java.util.Date;
import java.time.LocalDate;
class year {
//function to get the year
public static void getYear(String datecurrently){
LocalDate currentDate= LocalDate.parse(datecurrently);
// to get the year from the date
int year = currentDate.getYear();
System.out.println("The Year from the date is : " + year);
}
public static void main(String args[]){
String datecurrently = "2023-08-18";
getYear(datecurrently);
}
}
In the code above:
- The LocalDate package has been imported.
- Using the parse() method the specified year has been extracted and returned to the class.
- The date is entered and the get() method takes the year from the date.
Output

In the output above the year from the date has been printed.
Method 3: Using String.split() Method
This method splits the current date into a date at the zero indexes, a month at the first index, and a year at the second index.
class getyear {
public static void findtheyear(String currentdate)
{
// Separating the specified date by '-'
String seperate[] = currentdate.split("-");
//the date at position 0, month at 1, and year at 2
String year = seperate[2];
//Print the year from the given date
System.out.println("The year from the date is: " + year);
}
public static void
main(String args[])
{
String currentdate = "18-08-2024";
findyear(currentdate);
}
}
In the code above the split() method has been used to split the dates using a separator and the value 2 has been passed since the year has been placed in second position.
Output

The specified year has been printed using the split() method of Java.
Method 4: Using SimpleDateFormat Class
This method utilizes the built-in method of SimpleDateFormat and prints the year when yyyy is passed as an argument.
//import SimpleDateFormat
import java.text.SimpleDateFormat;
import java.util.Date;
//create a class to get the year
public class getyear {
public static void year(Date currentdate) {
// To get the year from SimpleDateFormat
SimpleDateFormat yeardate = new SimpleDateFormat("yyyy");
//return date
String year = yeardate.format(currentdate);
//print the year from the date
System.out.println("The year from the date is :" + year);
}
public static void main(String[] args) {
Date currentdate = new Date();
year(currentdate);
}
}
In the code above:
- The SimpleDateFormat has been imported to get the respective year.
- The format argument has been specified as yyyy for the year.
- Using the println() function the output has been printed on the screen,
Output

The current year has been printed as the output.
Conclusion:
There are various methods available in Java to fetch the year from the specified date. These methods and functions are flexible and easy to use. Using the Calendar class in Java, the Local Date class, the String.split() method and the SimpleDateFormat class, the year from the date can be easily derived. In this write-up, we demonstrated the use of different methods to get the year from the date in Java.