Year

Description

A Year represents a year, for example, 2012, 2013, etc.

Example

The following code shows how to create a Year object and perform basic operations on them.


import java.time.Year;
/*w  w w.  j  a  va  2 s  .  c o m*/
public class Main {

  public static void main(String[] args) {
    Year y1 = Year.of(2014);
    System.out.println(y1);
    Year y2 = y1.minusYears(1);
    System.out.println(y2);
    Year y3 = y1.plusYears(1);
    System.out.println(y3);
    Year y4 = Year.now();
    System.out.println(y4);
    if (y1.isLeap()) {
      System.out.println(y1 + "  is a  leap year.");
    } else {
      System.out.println(y1 + "  is not  a  leap year.");
    }

  }
}

The code above generates the following result.





















Home »
  Java Date Time »
    Tutorial »




Java Date Time Tutorial