Year类的until()方法用于使用TemporalUnit计算两个Year对象之间的时间量。起点和终点是此点,指定的Year作为参数传递。如果结束在开始之前,结果将为负。计算返回一个整数,代表两年之间的完整单位数。此实例是不可变的,不受此方法调用的影响。
用法:
public long until(Temporal endExclusive, TemporalUnit unit)
参数:此方法接受两个参数endExclusive,它是结束日期,exclusive,它转换为Year,单位是度量数量的单位。
返回值:此方法返回今年和结束年之间的时间量。
异常:此方法引发以下异常:
DateTimeException-如果无法计算数量,或者结束时态不能转换为Year。
UnsupportedTemporalTypeException-如果不支持该单元。
ArithmeticException-如果发生数字溢出。
以下示例程序旨在说明until()方法:
示例1:
// Java program to demonstrate
// Year.until() method
import java.time.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
// create Year objects
Year y1 = Year.of(2018);
Year y2 = Year.of(2015);
// apply until the method of Year class
long result
= y2.until(y1,
ChronoUnit.YEARS);
// print results
System.out.println("Result in YEARS: "
+ result);
}
}
输出:
Result in YEARS: 3
示例2:
// Java program to demonstrate
// Year.until() method
import java.time.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
// create Year objects
Year y1 = Year.of(2018);
Year y2 = Year.of(2200);
// apply until the method of Year class
long result
= y1.until(y2,
ChronoUnit.DECADES);
// print results
System.out.println("Result in DECADES: "
+ result);
}
}
输出:
Result in DECADES: 18