📜  Java8 Year Class

📅  最后修改于: 2020-10-01 06:38:05             🧑  作者: Mango

Java Year课程

Java Year类是代表一年的不可变日期时间对象。它继承了Object类并实现Comparable接口。

Java Year类声明

我们来看一下java.time.Year类的声明。

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

Java Year的方法

Method Description
LocalDate atDay(int dayOfYear) It is used to combine this year with a day-of-year to create a LocalDate.
String format(DateTimeFormatter formatter) It is used to format this year using the specified formatter.
int get(TemporalField field) It is used to get the value of the specified field from this year as an int.
boolean isAfter(Year other) It is used to check if this year is after the specified year.
boolean isBefore(Year other) It is used to check if this year is before the specified year.
boolean isLeap() It is used to check if the year is a leap year, according to the ISO proleptic calendar system rules.
int length() It is used to get the length of this year in days.
static Year now() It is used to obtain the current year from the system clock in the default time-zone.

Java Year示例:now()

import java.time.Year;
public class YearExample1 {
  public static void main(String[] args) {
    Year y = Year.now();
    System.out.println(y);
  }
}

输出:

2017

Java Year示例:atDay()

import java.time.LocalDate;
import java.time.Year;
public class YearExample2{
  public static void main(String[] args) {
    Year y = Year.of(2017);
    LocalDate l = y.atDay(123);
    System.out.println(l);
  }
}

输出:

2017-05-03

Java Year示例:length()

import java.time.Year;
public class YearExample3 {
  public static void main(String[] args) {
    Year year = Year.of(2017);
    System.out.println(year.length());
  }
}

输出:

365

Java Year示例:isLeap()

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

输出:

true