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
import java.time.LocalDate;
import java.time.Month;
import java.util.Date;
class GFG {
public static void getYear(String date)
{
LocalDate currentDate = LocalDate.parse(date);
int year = currentDate.getYear();
System.out.println( "Year: " + year);
}
public static void main(String args[])
{
String date = "2021-05-21" ;
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
import java.util.*;
class GFG {
public static void main(String args[])
{
Calendar Cal
= new GregorianCalendar(
2021 , 05 , 21 );
int year = Cal.get(Calendar.YEAR);
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
class GFG {
public static void findYear(String date)
{
String dateParts[] = date.split( "-" );
String year = dateParts[ 2 ];
System.out.println( "Year: " + year);
}
public static void
main(String args[])
{
String date = "21-05-2021" ;
findYear(date);
}
}
|
Output:
Year: 2021
Share your thoughts in the comments
Please Login to comment...