java 根据类名示例化类
即时类with()方法 (Instant Class with() method)
Syntax:
句法:
public Instant with(TemporalAdjuster t_adjuster);
public Instant with(TemporalField t_field, long new_val);
with() method is available in java.time package.
with()方法在java.time包中可用。
with(TemporalAdjuster t_adjuster) method is used to represent this Instant with the given adjuster.
with(TemporalAdjuster t_adjuster)方法用于用给定的调整器表示此Instant。
with(TemporalField t_field, long new_val) method is used to set the given value for the given field.
with(TemporalField t_field,long new_val)方法用于为给定字段设置给定值。
These methods may throw an exception at the time of representing Instant with the given adjustment.
在使用给定的调整量表示Instant时,这些方法可能会引发异常。
- DateTimeException: This exception may throw when this Instant couldn't adjust with the given adjuster.DateTimeException :如果此Instant无法使用给定的调整器进行调整,则可能引发此异常。
- ArithmeticException: This exception may throw when the calculated result exceeds the limit to represent this object.ArithmeticException :当计算结果超出表示此对象的限制时,可能引发此异常。
These are non-static methods and it is accessible with class objects and if we try to access these methods with the class name then we will get an error.
这些是非静态方法,可通过类对象访问,如果尝试使用类名访问这些方法,则会收到错误消息。
Parameter(s):
参数:
In the first case, "with(TemporalAdjuster t_adjuster)",
在第一种情况下,“ with(TemporalAdjuster t_adjuster)”
- TemporalAdjuster t_adjuster – represents the field in which to set the value the given value.
- TemporalAdjuster t_adjuster –表示在其中将值设置为给定值的字段。
In the second case, "with(TemporalField t_field, long new_val)",
在第二种情况下,“ with(TemporalField t_field,long new_val)”,
- TemporalField t_field – represents the field in which to set the value the given value.
- TemporalField t_field –表示将值设置为给定值的字段。
- long new_val – represents new value to set for the given field.
- long new_val –表示要为给定字段设置的新值。
Return value:
返回值:
In both the cases, the return type of the method is Instant, it returns the Instant that holds the value of this Instant with the given object.
在这两种情况下,方法的返回类型均为Instant ,它返回包含具有给定对象的该Instant值的Instant。
Example:
例:
// Java program to demonstrate the example
// of with() method of Instant
import java.time.*;
import java.time.temporal.*;
public class WithOfInstant {
public static void main(String args[]) {
long field_val = 10;
// Instantiates two Instant
Instant ins1 = Instant.parse("2006-04-03T05:10:15.00Z");
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("field_val to set: " + field_val);
System.out.println();
// Here, this method adjusts this Instant
// with the given adjuster
Instant with_val = ins1.with(ins2);
// Display with_val
System.out.println("ins1.with(ins2): " + with_val);
// Here, this method sets the new value
// for the given field in this Instant
with_val = ins1.with(ChronoField.MICRO_OF_SECOND, field_val);
// Display with_val
System.out.println("ins1.with(ChronoField.MICRO_OF_SECOND,field_val): " + with_val);
}
}
Output
输出量
Instant ins1 and ins2:
ins1: 2006-04-03T05:10:15Z
ins2: 2020-05-28T07:42:39.007362Z
field_val to set: 10
ins1.with(ins2): 2020-05-28T07:42:39.007362Z
ins1.with(ChronoField.MICRO_OF_SECOND,field_val): 2006-04-03T05:10:15.000010Z
翻译自: https://www.includehelp.com/java/instant-with-method-with-example.aspx
java 根据类名示例化类