BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Year class explained with examples

By Chaitanya Singh | Filed Under: 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 Introduction

  • 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 Flow Control

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

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • 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 Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap