Get Year From Date in Java

Rupam Yadav Jan 30, 2023 Jan 26, 2021
  1. Get the Current Year Using Date and LocalDate in Java
  2. Get the Current Year Using Date and SimpleDateFormat in Java
  3. Get the Current Year Using Date.getYear() and Calendar
Get Year From Date in Java

This tutorial goes through the ways to get the current year in Java. We will use the java.util.Date class to get the current date and then extract out the current year using several methods.

Get the Current Year Using Date and LocalDate in Java

java.util.Date has the getyear() method that returns a value that is subtracted from the year 1900. But this method was deprecated a long while ago in Java. Instead, we can use the LocalDate class available in java.time as the preferred method to do any Date and Time operations.

We convert the date object to a LocalDate object by using date.toInstant() that returns the instant of the time. Next, we call atZone() to specify the system’s default timezone using ZoneId.systemDefault() and passing its value to atZone's constructor. At last, we call toLocalDate() to convert the instant value to LocalDate.

Now that we have a LocalDate object getLocalDate we get the year using getLocalDate.getYear().

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;

public class DateGetYear {
    public static void main(String[] args) {
        Date date = new Date();

        ZoneId timeZone = ZoneId.systemDefault();
        LocalDate getLocalDate = date.toInstant().atZone(timeZone).toLocalDate();
        System.out.println(getLocalDate.getYear());

    }
}

Output:

2021

Get the Current Year Using Date and SimpleDateFormat in Java

Date returns both the date and time in which the time is set to 00:00:00. To get only the year, we format the date using SimpleDateFormat. We create a SimpleDateFormat object and pass in the format i.e. yyyy. we call format() and use the date as the argument. getYearFormat.format(date) returns the result in a string as shown in the output.

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateGetYear {
    public static void main(String[] args) {
        Date date = new Date();

        SimpleDateFormat getYearFormat = new SimpleDateFormat("yyyy");
        String currentYear = getYearFormat.format(date);
        System.out.println(currentYear);
        
    }
}
2021

Get the Current Year Using Date.getYear() and Calendar

The last method uses the Calendar class that is said to replace some methods of java.util.Date. We create a Date object and get an instance of calendar using calendar.getInstance(). After that, we call calendar.setTime() method to set Calendar’s time using date.

Now to get the year, we call calendar.get() that returns a value of the field passed as its argument. We pass Calendaer.YEAR to get the year as int.

import java.util.Calendar;
import java.util.Date;

public class DateGetYear {
    public static void main(String[] args) {

        Date date = new Date();
        Calendar calendar = Calendar.getInstance();

        calendar.setTime(date);
        int dateYear = calendar.get(Calendar.YEAR);
        System.out.println(dateYear);

    }
}

Output:

2021
Author: Rupam Yadav
Rupam Yadav avatar Rupam Yadav avatar

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.

LinkedIn

Related Article - Java DateTime