Java Year

The Java Year class is an immutable and thread-safe class that represents a year. The Year class can be used to get any field that can be derived from the year.

How to use

The Year class does not store or represent the month, day, time, or time zone. For example, you can store the value “2019” in Year. Try the following examples to better understand –

Now example

The following example program shows how you can use Year class to get the current year.

import java.time.Year;

public class YearExample1 {

    public static void main(String[] args) {

        // Create instance of Year
        Year year = Year.now();

        // Print the current year
        System.out.println(year);
    }
}

AtDay example

The following example program shows how you can get the date of a particular day of the year.

import java.time.LocalDate;
import java.time.Year;

public class YearExample2 {

    public static void main(String[] args) {

        // Create instance of Year
        Year year = Year.now();

        // Get the 10th day of the year
        LocalDate localDate = year.atDay(10);
        System.out.println(localDate);
    }
}

Leap year example

The following example program shows how you can use Year class to check if the year is a leap year or not.

import java.time.Year;

public class YearExample3 {

    public static void main(String[] args) {

        // Create instance of Year
        Year year = Year.now();

        // Check is leap year or not
        System.out.println(year.isLeap());
    }
}

Total days example

The following example program shows how you can use Year class to get the total number of days in the year.

import java.time.Year;

public class YearExample4 {

    public static void main(String[] args) {

        // Create instance of Year
        Year year = Year.now();

        // Get total number of days in current year
        System.out.println(year.length());
    }
}