Get Year From Date in Java

  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

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
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Java Date

  • Get Today's Date in Java
  • Add One Day to a Date in Java
  • Related Article - Java Year

  • Get Today's Date in Java