java 根据类名示例化类
即时类truncatedTo()方法 (Instant Class truncatedTo() method)
truncatedTo() method is available in java.time package.
truncatedTo()方法在java.time包中可用。
truncatedTo() method is used to get an Instant that holds the value of this Instant truncated to the given unit.
truncatedTo()方法用于获取一个Instant,该Instant保留被截断到给定单位的Instant的值。
truncatedTo() 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.
truncatedTo()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
truncatedTo() method may throw an exception at the time of truncating.
truncatedTo()方法在截断时可能会引发异常。
- DateTimeException: This exception may throw when the given unit can't truncate due to an invalid unit.DateTimeException :当给定的单元由于无效的单元而无法截断时,可能引发此异常。
- UnsupportedTemporlTypeException: This exception may throw when the given unit is unsupported.
- UnsupportedTemporlTypeException : 不支持给定单元时,可能会引发此异常。
Syntax:
句法:
public Instant truncatedTo(TemporalUnit t_unit);
Parameter(s):
参数:
TemporalUnit t_unit – represents the unit to truncate from this Instant.
TemporalUnit t_unit –表示要从此Instant截断的单位。
Return value:
返回值:
The return type of this method is Instant, it returns the Instant that holds the value truncated the given unit from this Instant.
此方法的返回类型为Instant ,它返回Instant,该Instant保存从该Instant截断给定单位的值。
Example:
例:
// Java program to demonstrate the example
// of truncatedTo(TemporalUnit t_unit) method
// of Instant
import java.time.*;
import java.time.temporal.*;
public class TruncatedToOfInstant {
public static void main(String args[]) {
// Instantiates two Instant
Instant ins1 = Instant.parse("2006-04-03T05:10:15.60Z");
Instant ins2 = Instant.now();
// 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 truncates the given
// unit from this Instant i.e. here
// we are truncating SECONDS unit
// from this Instant ins1
Instant ins = ins1.truncatedTo(ChronoUnit.SECONDS);
// Display ins
System.out.println("ins1.truncatedTo(ChronoUnit.SECONDS): " + ins);
// Here, this method truncates the given
// unit from this Instant i.e. here
// we are truncating MINUTES unit
// from this Instant ins2
ins = ins2.truncatedTo(ChronoUnit.MINUTES);
// Display ins
System.out.println("ins2.truncatedTo(ChronoUnit.MINUTES): " + ins);
}
}
Output
输出量
Instant ins1 and ins2:
ins1: 2006-04-03T05:10:15.600Z
ins2: 2020-05-27T06:11:43.033792Z
ins1.truncatedTo(ChronoUnit.SECONDS): 2006-04-03T05:10:15Z
ins2.truncatedTo(ChronoUnit.MINUTES): 2020-05-27T06:11:00Z
翻译自: https://www.includehelp.com/java/instant-truncatedto-method-with-example.aspx
java 根据类名示例化类