BeginnersBook

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
    • Learn jQuery
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java Year class explained with examples

By Chaitanya Singh | Filed Under: Learn Java

An instance of Year class represents the year ISO-8601 calendar system, such as 2022. Any field that can be derived from a year, can be obtained using this class. This class doesn’t store day, month, time or timezone information, for example value “2022” can be stored in Year, however the value “2nd October 2022” cannot be stored in Year.

Year class:

public final class Year extends Object
implements Temporal, TemporalAdjuster, Comparable<Year>, Serializable

Java Year class – Method Summary

MethodDescription
LocalDate atDay(int day)It combines this year with a day-of-year to create an instance of LocalDate.
boolean isLeap()Checks whether this Year is a Leap year or not. Returns true or false based on the result.
int length()This method returns the length of the year in number of days.
static Year now()Fetches the current year from system in default time zone.
boolean isAfter(Year otherYear)It checks if this year is after the specified year.
boolean isBefore(Year otherYear)It checks if this year is before the specified year.
String format(DateTimeFormatter formatter)Formats the Year using the specified formatter.
int get(TemporalField field)Returns the value of the specified field from this year.
boolean equals(Object obj)Checks if this year is equal to other specified year.
int compareTo(Year other)Compares this year to another Year and return int value. The difference between equals() and this method is that the equals() method returns true or false. However this method 0 if the years are equal, negative if this year is less than specified year and positive value if this year is greater than specified year.
YearMonth atMonth(Month month)Combines this year with the specified month to create YearMonth

Java Year – now() and length() methods example

Method now() is used to get the current year from the system in default time-zone. Method length() returns the number of days in this year.

import java.time.Year;
public class JavaExample {
  public static void main(String[] args) {
    Year year = Year.now();
    System.out.println("Current Year: "+year);

    int length = year.length();
    System.out.println("Length of the year: "+length);
  }
}

Output:

Current Year: 2022
Length of the year: 365

Java Year -isLeap() method example

In the following example, we are using isLeap() method to check if the specified year is leap year or not. This method return true if the year is leap year else it returns false. As you see in this example, this method returned false for year “2022” and returned true for year “2020“.

import java.time.Year;
public class JavaExample {
  public static void main(String[] args) {
    Year year = Year.of(2022);
    System.out.println(year.isLeap());
    Year year2 = Year.of(2020);
    System.out.println(year2.isLeap());
  }
}

Output:

false
true

Java Year – atDay() & atMonth() example

We can add day of the year or month to this Year so that we can get Local date or an instance of YearMonth. Here atDay() method is used to combine the number of days to the year so that we can get an instance of LocalDate.

Method atMonth() combine number of months to this year to return YearMonth.

import java.time.LocalDate;
import java.time.Year;
import java.time.YearMonth;
public class JavaExample{
  public static void main(String[] args) {
    Year year = Year.of(2022);
    LocalDate localDate = year.atDay(150);
    System.out.println(localDate);
    YearMonth yearMonth = year.atMonth(4);
    System.out.println(yearMonth);
  }
}  

Output:

2022-05-30
2022-04

Reference:

Javadoc

❮ Java Tutorial

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Control Statements

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor
  • Java Interview Q

MORE ...

  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java String
  • Java Date
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap