Java Program to Get Year from Date


In our day to day life, there is a huge data being generated in this real world. Among the information generated, Dates are generally used for various things such as scheduling an appointment, planning for day-to-day activities. While working with dates in programming, extracting the year from a specific date can be a common task. We can extract the Year from a particular Date using different in-built functions in Java programming language. In this article we will be discussing different approaches for extracting year from a particular date.

Example
Input: "13-07-2000"
Output: 2000
Example
Input: "1997/07/13"
Output: 1997

Approaches

  • Using LocalDate Class.

  • Using String.split() method.

  • Using Calender Class.

  • Using SimpleDateFormat Class.

Algorithm

  • Write a custom method in the Main class to extract Year from date.

  • Use different inbuilt functions provided by java in the custom functions to extract date.

  • Print the Year extracted from date.

We will now discuss each approach in detail for extraction of year from date and implement code in java programming language.

Approach 1: Using LocalDate Class

In this approach, we will use the LocalDate class from the java.time package.The method ‘getYear()’ from LocalDate. getYear() method generally returns an integer as output.

Syntax

The following syntax refers to the usage of getYear() method.

int variable = DateObject.getYear();

Here a simple code snippet using the above syntax:

LocalDate currentDate = LocalDate.parse("2000-07-13");
int year = currentDate.getYear();
System.out.println(year); // gives 2000 as output     

Example

In this program we will import the LocalDate class of the java.time package. Then we need to parse the string into date using parse() method. The parse() method accepts date in ‘yyyy-mm–dd’ format and it converts a string into local date. Once we get date we can extract year using getYear() method.

// Java program for extracting Year from Date
import java.util.*;
import java.time.LocalDate;
public class Main {
   public static void getYearFromDate(String date) {
      LocalDate inputDate = LocalDate.parse(date);
      // Get year from date
      int year = inputDate.getYear();
      System.out.println("Year extracted from date is : " + year);
   }
   public static void main(String args[]) {
      String date = "2000-07-13";
      getYearFromDate(date);
   }
}

Output

Year extracted from date is : 2000    

Time Complexity: O(1) Auxiliary Space: O(1)

Approach 2: Using String.split() method

In this approach, we will use the split() method from the String class. This method is used to divide a string into tokens based on a delimiter (the symbol which is using to divide the string into tokens).

Syntax

The following syntax refers to the usage of split() method.It takes a delimiter as an input and give array of string tokens as output.

String array_variable [] = String_Variable.split(“delimiter”);   

Here is a simple code example demonstrating the above syntax:

date  = '13-07-2000';
String tokens[] = date.split("-");
System.out.println(tokens[2]); 
// gives 2000 as output

Example

In the above program, a string is declared and passed to extractYear() function. This function generally uses the split() method of String class. The split() method generally takes a delimiter(in the above program it is “-”) and splits the string into an array of Strings .As year is the 3rd token in the date, we print tokens[2] which gives the desired output.

// Java program for  extracting year from the given date
import  java.util.*;
public class Main {
   public static void extractYear(String date) {
      // Splitting the given date by '-'
      String tokens[] = date.split("-");
      String year = tokens[2];
      System.out.println("Extracted Year from Date: " + year);
   }
   public static void main(String args[]) {
      // Given date
      String date = "13-07-2000";
      extractYear(date);
   }
}

Output

Extracted Year from Date: 2000

Time Complexity: O(1) Auxiliary Space: O(1)

Approach 3: Using Calendar Class

In this approach, we will use the Calendar class from the util package. We in this example use Calendar.YEAR which extracts Year in a particular date using Calendar object.

Syntax

The following syntax refers to the usage of get() method of Calender class . It returns a integer value specified as a field_parameter.

int var_name = CalenderObject.get(field_parameter);

Here a simple code snippet that uses the above syntax to get the year from date −

Calendar cal = Calendar.getInstance(); // returns  Calender Object with present date
int Year = cal.get(Calendar.YEAR); // gives the year from Calender Object

Example

In the above example we are using Calendar class to get the year from date. We declare a calendar object and use getInstance() method of Calendar class to get current date object. We use get() method and pass Calendar.YEAR parameter to get the year from the date.

import java.util.Calendar;
public class Main {
   public static void getYear(Calendar cal){
      int Year = cal.get(Calendar.YEAR);
      System.out.println("Extracted year from date : " +Year);
   }
   public static void main(String[] args) {
      Calendar cal = Calendar.getInstance();
      getYear(cal);
   }
}	

Output

Extracted year from date : 2023

Time Complexity: O(1) Auxiliary Space: O(1)

Approach 4: Using SimpleDateFormat Class

In this approach, we will use the SimpleDateFormat class from the text package and Date class from util package. We in this example use Date class and get a date initially, we then create a SimpleDateFormat object which generally formats a date to a specified format as given in parameter.

Syntax

The following syntax refers to the usage of format() method of SimpleDateFormat class . It returns a integer value specified as a field_parameter.

int var_name = CalenderObject.get(field_parameter);

Here is an implementation of the above syntax:

Calendar cal = Calendar.getInstance(); 
// returns Calendar Object with present date
int Year = cal.get(Calendar.YEAR); // gives the year from Calendar Object

Example

In this example, we used Date and SimpleDateFormat classes. SimpleDateFormat is used to get only the date from the date object as date gives both date and time. So, we first declare a date object and then create and SimpleDateFormat object to get year by passing ‘yyyy’ parameter.format() method it is used to return a date or time using given pattern.

import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
   public static void getYear(Date date) {
      SimpleDateFormat getYearDate = new SimpleDateFormat("yyyy");
      String year = getYearDate.format(date);
      System.out.println("Extracted year from date :" + year);
   }
   public static void main(String[] args) {
      Date date = new Date();
      getYear(date);
   }
}

Output

Extracted year from date :2023

Time Complexity: O(1) Auxiliary Space: O(1)

Thus, in this article we have discussed different approaches to implement a java program for extracting year from a specific date.

Updated on: 11-Apr-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements