java 根据类名示例化类
即时类直到()方法 (Instant Class until() method)
until() method is available in java.time package.
在java.time包中可以使用until()方法 。
until() method is used to determine the amount of time until other Instant in terms of the given unit.
直到指定的单位为止,直到直到其他Instant的时间量为止,使用until()方法来确定。
until() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
till()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
until() method may throw an exception at the time of representing an Instant.
直到表示Instant时, until()方法可能会引发异常。
- DateTimeException: This exception may throw when the given temporal can’t convert into this Instant.DateTimeException :当给定的时间不能转换为该Instant时,可能抛出此异常。
- UnsupportedTemporlTypeException: This exception may throw when the given unit is unsupported.
- UnsupportedTemporlTypeException : 不支持给定单元时,可能会引发此异常。
Syntax:
句法:
public long until(Temporal en_ex, TemporalUnit t_unit);
Parameter(s):
参数:
Temporal en_ex – represents the ending temporal object to convert to an Instant.
时间en_ex –表示要转换为Instant的结束时间对象。
TemporalUnit t_unit – represents the unit of the returned amount of time.
TemporalUnit t_unit –表示返回的时间量的单位。
Return value:
返回值:
The return type of this method is long, it returns the amount of time between this Instant and the given Instant.
此方法的返回类型为long ,它返回此Instant和给定Instant之间的时间量。
Example:
例:
// Java program to demonstrate the example
// of until(Temporal en_ex, TemporalUnit t_unit)
// method of Instant
import java.time.*;
import java.time.temporal.*;
public class UntilOfInstant {
public static void main(String args[]) {
// Instantiates two Instant
Instant ins1 = Instant.parse("2006-04-03T20:10:30.60Z");
Instant ins2 = Instant.parse("2006-04-03T10:10:15.60Z");
// Display ins1,ins2
System.out.println("Instant ins1 and ins2: ");
System.out.println("ins1: " + ins1);
System.out.println("ins2: " + ins2);
System.out.println();
// Here, this method determines the amount of
// time of this Instant (ins2) until
// another Instant (ins1) in the
// given unit (SECONDS)
long until = ins2.until(ins1, ChronoUnit.SECONDS);
// Display until
System.out.println("ins2.until(ins1,ChronoUnit.SECONDS): " + until);
// Here, this method determines the amount of
// time of this Instant (ins2) until
// another Instant (ins1) in the
// given unit (HOURS)
until = ins2.until(ins1, ChronoUnit.HOURS);
// Display until
System.out.println("ins2.until(ins1,ChronoUnit.HOURS): " + until);
}
}
Output
输出量
Instant ins1 and ins2:
ins1: 2006-04-03T20:10:30.600Z
ins2: 2006-04-03T10:10:15.600Z
ins2.until(ins1,ChronoUnit.SECONDS): 36015
ins2.until(ins1,ChronoUnit.HOURS): 10
翻译自: https://www.includehelp.com/java/instant-until-method-with-example.aspx
java 根据类名示例化类