本文是《Java日期时间API教程》系列。 在这篇文章中,我们将讨论java.time package中的Java Year类的重要方法或API。Year
类是一个不可变的日期时间对象,它代表一个年份。任何可以从年衍生出来的字段都可以得到。这个类是不可变的,并且是线程安全的。
下面的类图显示了Year
类所提供的方法列表。
让我们用例子来证明几个重要和常用的Year
类方法的用法。在下面的YearMethodsExample类中,每个方法的名称都描述了Year
类方法及其用法。请参考每个方法的注释是自述的。
package net.javaguides.date;
import java.time.Clock;
import java.time.LocalDate;
import java.time.Year;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalQueries;
//*/*
/* Class demonstrates the usage of Year methods with examples
/*
/* @author Ramesh Fadatare
/*
/*/
public class YearMethodsExample {
public static void main(String[] args) {
YearMethodsExample example = new YearMethodsExample();
example.withMethodExample();
example.untilMethodExample();
example.toStringMethodExample();
example.rangeMethodExample();
example.queryMethodExample();
example.plusYearsMethodExample();
example.ofMethodExample();
example.nowMethod();
example.now();
example.nowMethodExample();
example.minusYearsMethodExample();
example.minusMethodExample();
example.isLeapMethodExample();
example.isBeforeMethodExample();
example.isAfterMethodExample();
example.getMethodExample();
example.fromMethodExample();
example.formatMethodExample();
example.atDayMethodExample();
}
//*/*
/* Demonstration of Year.parse(CharSequence text)
/*/
public void withMethodExample() {
Year date = Year.parse("2017");
Year result = date.with(Year.of(2010));
System.out.println(result);
}
//*/*
/* Demonstration of Year.until(Temporal endExclusive, TemporalUnit unit)
/*/
public void untilMethodExample() {
Year date = Year.parse("2015");
Year date1 = Year.now();
System.out.println(date.until(date1, ChronoUnit.YEARS));
}
//*/*
/* Demonstration of Year.toString()
/*/
public void toStringMethodExample() {
Year date = Year.parse("2017");
System.out.println(date.toString());
}
//*/*
/* Demonstration of Year.range(TemporalField field)
/*/
public void rangeMethodExample() {
Year date = Year.parse("2014");
System.out.println("Range : " + date.range(ChronoField.YEAR_OF_ERA));
}
//*/*
/* Demonstration of Year.query(TemporalQuery<TemporalUnit> query)
/*/
public void queryMethodExample() {
Year date = Year.parse("2014");
System.out.printf("Year precision is %s%n", date.query(TemporalQueries.precision()));
}
//*/*
/* Demonstration of Year.plusYears(long yearsToAdd)
/*/
public void plusYearsMethodExample() {
Year date = Year.parse("2017");
Year date1 = date.plusYears(10);
System.out.println(date1);
}
//*/*
/* Demonstration of Year.of(int isoYear)
/*/
public void ofMethodExample() {
Year year = Year.of(2017);
System.out.println(year);
}
//*/*
/* Demonstration of Year.now(ZoneId zone)
/*/
public void nowMethod() {
Year date = Year.now(ZoneId.systemDefault());
System.out.println(date);
}
//*/*
/* Demonstration of Year.now(Clock clock)
/*/
public void now() {
Year date = Year.now(Clock.systemUTC());
System.out.println(date);
}
//*/*
/* Demonstration of Year.now()
/*/
public void nowMethodExample() {
Year date = Year.now();
System.out.println(date);
}
//*/*
/* Demonstration of Year.minusYears(long yearsToSubtract)
/*/
public void minusYearsMethodExample() {
Year date = Year.of(2017);
System.out.println(date.get(ChronoField.YEAR));
System.out.println(date.minusYears(4).get(ChronoField.YEAR));
}
//*/*
/* Demonstration of Year.minus(long amountToSubtract, TemporalUnit unit)
/*/
public void minusMethodExample() {
Year date = Year.of(2017);
System.out.println(date.minus(2, ChronoUnit.YEARS));
}
//*/*
/* Demonstration of Year.isLeap(long year)
/*/
public void isLeapMethodExample() {
System.out.println(Year.isLeap(2016));
}
//*/*
/* Demonstration of Year.isBefore(Year other)
/*/
public void isBeforeMethodExample() {
Year date = Year.of(2016);
Year date1 = Year.of(2017);
System.out.println(date1.isBefore(date));
}
//*/*
/* Demonstration of Year.isAfter(Year other)
/*/
public void isAfterMethodExample() {
Year date = Year.of(2016);
Year date1 = Year.of(2017);
System.out.println(date1.isAfter(date));
}
//*/*
/* Demonstration of Year.get(TemporalField field)
/*/
public void getMethodExample() {
Year date = Year.of(2017);
System.out.println(date.get(ChronoField.YEAR_OF_ERA));
}
//*/*
/* Demonstration of Year.from(TemporalAccessor temporal)
/*/
public void fromMethodExample() {
Year date = Year.from(Year.of(2017));
System.out.println(date);
}
//*/*
/* Demonstration of java.time.format.DateTimeFormatter.format(TemporalAccessor temporal)
/*/
public void formatMethodExample() {
Year date = Year.of(2017);
System.out.println(date);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yy");
System.out.println(formatter.format(date));
}
//*/*
/* Demonstration of Year.atDay(int dayOfYear)
/*/
public void atDayMethodExample() {
Year year = Year.of(2015);
LocalDate date = year.atDay(26);
System.out.println(date);
}
}
输出:
2010
3
2017
Range : 1 - 999999999
Year precision is Years
2027
2017
2018
2018
2018
2017
2013
2015
true
false
true
2017
2017
2017
17
2015-01-26
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://www.javaguides.net/2018/12/java-year-class-methods-with-examples.html
内容来源于网络,如有侵权,请联系作者删除!