Java8的时间类Year,只用来表示年,例如:2021。
import java.time.LocalDate;
import java.time.MonthDay;
import java.time.Year;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalQueries;
public class YearTest {
public static void main(String[] args) {
LocalDate localDate = LocalDate.of(2020,1,2);
Year now = Year.now();
System.out.println("now:"+now);
//adjustInto
System.out.println(now.adjustInto(localDate));
//atDay
LocalDate atDay = now.atDay(105);
System.out.println("atDay:"+atDay);
//atMonth
System.out.println("atMonth:"+now.atMonth(5));
//atMonthDay
MonthDay monthDay = MonthDay.now();
System.out.println("atMonthDay:"+Year.of(2010).atMonthDay(monthDay));
//compareTo
int i = now.compareTo(Year.of(2010));
System.out.println("compareTo:"+i);
i = Year.of(2010).compareTo(now);
System.out.println("compareTo:"+i);
//isAfter
boolean after = now.isAfter(Year.of(2010));
System.out.println("isAfter:"+after);
//isBefore
boolean before = now.isBefore(Year.of(2010));
System.out.println("isBefore:"+before);
//isLeap
boolean leap = now.isLeap();
System.out.println("isLeap:"+leap);
//isSupported
System.out.println("isSupported:"+now.isSupported(ChronoUnit.YEARS));
System.out.println("isSupported:"+now.isSupported(ChronoUnit.MONTHS));
System.out.println("isSupported:"+now.isSupported(ChronoUnit.DAYS));
//get
System.out.println("get:"+now.get(ChronoField.YEAR));
//System.out.println("get:"+now.get(ChronoField.DAY_OF_YEAR));
//System.out.println("get:"+now.get(ChronoField.DAY_OF_MONTH));
//System.out.println("get:"+now.get(ChronoField.DAY_OF_WEEK));
//System.out.println("get:"+now.get(ChronoField.NANO_OF_SECOND));
//System.out.println("get:"+now.get(ChronoField.SECOND_OF_MINUTE));
//isValidMonthDay
MonthDay monthDay2 = MonthDay.of(2, 29);
System.out.println("isValidMonthDay 2010:"+Year.of(2010).isValidMonthDay(monthDay2));
System.out.println("isValidMonthDay 2011:"+Year.of(2011).isValidMonthDay(monthDay2));
System.out.println("isValidMonthDay 2012:"+Year.of(2012).isValidMonthDay(monthDay2));
System.out.println("isValidMonthDay 2013:"+Year.of(2013).isValidMonthDay(monthDay2));
//parse
System.out.println("parse:"+Year.parse("2010"));
//plusYears
System.out.println("plusYears:"+now.plusYears(10));
//query
System.out.println("query:"+now.query(TemporalQueries.chronology()));
System.out.println("query:"+now.query(TemporalQueries.precision()));
//range
System.out.println("query:"+now.range(ChronoField.YEAR));
//until
LocalDate localDate1 = LocalDate.now().withYear(2050);
System.out.println("until:"+now.until(localDate1, ChronoUnit.YEARS));
//with
LocalDate localDate2 = LocalDate.now().withYear(2050);
System.out.println("with:"+localDate2);
}
}
输出:
now:2021
2021-01-02
atDay:2021-04-15
atMonth:2021-05
atMonthDay:2010-07-16
compareTo:11
compareTo:-11
isAfter:true
isBefore:false
isLeap:false
isSupported:true
isSupported:false
isSupported:false
get:2021
isValidMonthDay 2010:false
isValidMonthDay 2011:false
isValidMonthDay 2012:true
isValidMonthDay 2013:false
parse:2010
plusYears:2031
query:ISO
query:Years
query:-999999999 - 999999999
until:29
with:2050-07-16