Java Year类使用示例教程

x33g5p2x  于2021-08-22 转载在 Java  
字(4.7k)|赞(0)|评价(0)|浏览(190)

本文是《Java日期时间API教程》系列。 在这篇文章中,我们将讨论java.time package中的Java Year类的重要方法或API。
Year类是一个不可变的日期时间对象,它代表一个年份。任何可以从年衍生出来的字段都可以得到。这个类是不可变的,并且是线程安全的

Java年份类图

下面的类图显示了Year类所提供的方法列表。

Java年类的方法/API与实例

让我们用例子来证明几个重要和常用的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

相关文章

热门文章

更多