Java Program to Get Year From Date
Last Updated :
09 Jul, 2021
Java is the most powerful programming language, by which we can perform many tasks and Java is an industry preferable language. So it is filled with a huge amount of features. Here we are going to discuss one of the best features of Java, which is how to get a year from date using Java.
Methods:
There are many ways to get a year from date of which frequently used two methods are listed below.
- Using get() method LocalDate class
- Using get() method of Calendar class
- Using split() method of String class
Let us discuss each of them in detail alongside implementing the methods
Method 1: Using get() method of LocalDate class
The get() method of LocalDate class in Java method gets the value of the specified field from this Date as an int.
Syntax:
public int get(TemporalField field)
Parameter: This method accepts a parameter field which is the field to get and not necessarily null.
Return Value: It returns the value for the field.
Exceptions: It throws three exceptions namely as follows:
- DateTimeException: This exception is thrown if a value for the field cannot be obtained or the value is outside the range of valid values for the field.
- UnsupportedTemporalTypeException: This exception is thrown if the field is not supported or the range of values exceeds an int
- ArithmeticException: This exception is thrown if numeric overflow occurs
Example
Java
// Java Program to Get Year From Date
// Using LocalDate class
// Importing Classes/Files
import java.time.LocalDate;
import java.time.Month;
import java.util.Date;
// Main class
class GFG {
// Method 1
// To get the year
public static void getYear(String date)
{
// Getting an instance of LocalTime from date
LocalDate currentDate = LocalDate.parse(date);
// Getting year from date
int year = currentDate.getYear();
// Printing the year
System.out.println("Year: " + year);
}
// Method 2
// Main driver method
public static void main(String args[])
{
// Specifying a date
String date = "2021-05-21";
// Function Call
getYear(date);
}
}
Output:
Year: 2021
Method 2: Using get() method of Calendar class
The get(int field_value) method of Calendar class is used to return the value of the given calendar field in the parameter.
Syntax:
public int get(int field)
Parameters: The method takes one parameter field_value of integer type and refers to the calendar whose value is needed to be returned.
Return Value: The method returns the value of the passed field.
Note: In above program, get() method is used to get the year from the specified date.
Example
Java
// Java Program to Get Year From Date
// using Calendar class
// Importing Classes/Files
import java.util.*;
// main class
class GFG {
// Main Driver Code
public static void main(String args[])
{
// Creating a calendar object
Calendar Cal
= new GregorianCalendar(
2021, 05, 21);
// Getting the values of year from calendar
// object
int year = Cal.get(Calendar.YEAR);
// Printing the year value
System.out.println("Year: " + year);
}
}
Output:
Year: 2021
Method 3: Using split() method of String class
This method does break a given string around matches of the given regular expression.
Parameters: It takes two parameters namely:
- Regex: A delimiting regular expression
- Limit: The resulting threshold
Return type: An array of strings computed by splitting the given string.
Exceptions: PatternSyntaxException - if the provided regular expression’s syntax is invalid.
Here split() function of String class is used to split a specified date string according to the given pattern, and it returns an array of string.
Example
Java
// Java Program to Get Year From Date
// Using String.split() method
// Main class
class GFG {
// Method 1
// To get year from date
public static void findYear(String date)
{
// Splitting the given date by '-'
String dateParts[] = date.split("-");
// Getting year from date
String year = dateParts[2];
// Printing the year
System.out.println("Year: " + year);
}
// Method 2
// Main Driver Code
public static void
main(String args[])
{
// Given date
String date = "21-05-2021";
// Function calling
findYear(date);
}
}
Output:
Year: 2021
Similar Reads
How to get Day, Month and Year from Date in Java Given a date in the form of a string, the task is to write a Java Program to get the day, month, and year from the given date. Examples: Input: date = "2020-07-18"Output:Day: 18Month: JulyYear: 2020Explanation: The given date is '2020-07-18', so the day is: 18, the month is: July, and the year is: 2
4 min read
Java Program to Display Dates of a Calendar Year in Different Format As different countries do opt for different formats. So here the goal is simply to print dates of calendar in different years. The generic symbolic notation opted across the globe is: Symbolic NotationDepictsyyearMmonth in year dday in month Eday of week Concept: Whenever it comes down to the date a
4 min read
Java Program to Convert Date to String The date type object can be converted to a string in Java using a large variety of in-built classes available in Java. Given a date, we need to convert the date to a given format of the string. Examples: Input: date = â2020-11-13âOutput: 2020-11-13 Input: date = â2020-12-14âOutput: 2020-12-14 Method
3 min read
How to convert Date to String in Java Given a date, the task is to write a Java program to convert the given date into a string. Examples: Input: date = "2020-07-27" Output: 2020-07-27 Input: date = "2018-02-17" Output: 2018-02-17 Method 1: Using DateFormat.format() method Approach: Get the date to be converted.Create an instance of Sim
3 min read
How to Calculate the Week Number of the Year for a Given Date in Java? In Java, we have the Calendar class and the newer LocalDate class from java.time package. We can use these classes to find which week number of the year for a given date. The java.time package offers better functionality than the older date and time classes such as Calendar class. In this article, w
3 min read