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
Java Program to Get Today's Date
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 today's or current date using Java. Metho
2 min read
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 Find if a Given Year is a Leap Year
Leap Year contains 366 days, which comes once every four years. In this article, we will learn how to write the leap year program in Java. Facts about Leap YearEvery leap year corresponds to these facts : A century year is a year ending with 00. A century year is a leap year only if it is divisible
5 min read
Java Program Format Time in MMMM Format
In Java, The MMM format for months is a short form of the month using 3 characters. SimpleDateFormat class is used for implementing this format in java and you can import this package by using the following code. import java.text.SimpleDateFormat Example: Months Name(MMMM Format)MMM FormatJanuaryJan
2 min read
Java Program to Extract Last two Digits of a Given Year
As the name suggests where there is an execution to be operated required to deal with digits of the number, the modulo operator plays a vital role. Here the goal is to extract the last digits of a number. So making the problem easier to think about what if the goal is to extract the last digit from
3 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
Java Program to Generate Calendar of Any Year Without calendar.get() Function
Java program for generating the calendar of any desired year and month let us first go through an illustration before landing upon logic and procedural part. Illustration: Say the user wants to get the calendar of April 2011. Then, he is required to enter the year along with the month as integers an
4 min read
Java Program to Convert TimeStamp to Date
Date Time class is used to display date and time and manipulate date and time in java and in addition to this it is also used for formatting date and time class in java across time zone associated data. So in order to import this class from a package called java.utils Timestamp class can be converte
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