本文共 3743 字,大约阅读时间需要 12 分钟。
java 根据类名示例化类
Syntax:
句法:
public static Instant ofEpochSecond(long sec_val); public static Instant ofEpochSecond(long sec_val, long nanos_adjust);
ofEpochSecond() method is available in java.time package.
ofEpochSecond()方法在java.time包中可用。
ofEpochSecond(long sec_val) method is used to represent an instance of this Instant by using the given seconds from the java epoch standard format since 1970-01-01T00:00:00Z.
ofEpochSecond(long sec_val)方法用于表示此Instant的实例, 方法是使用从1970-01-01T00:00:00Z开始的java epoch标准格式的给定秒数。
ofEpochSecond(long sec_val, long nanos_adjust) method is used to represent an instance of this Instant by using the given seconds and nano fraction of seconds from the java epoch of 1970-01-01T00:00:00Z.
ofEpochSecond(long sec_val,long nanos_adjust)方法用于通过使用距1970-01-01T00:00:00Z的java纪元的给定秒数和秒的毫微秒数来表示此Instant的实例。
These methods may throw an exception at the time of representing seconds in epoch format.
这些方法在以纪元格式表示秒时可能会引发异常。
DateTimeException: This exception may throw when this Instant value reaches out of the min or max instant.
DateTimeException :当此Instant值超出最小或最大瞬时值时,可能引发此异常。
These are static methods and it is accessible with class name and if we try to access these methods with class object then we will not get an error.
这些是静态方法,可通过类名进行访问,如果尝试使用类对象访问这些方法,则不会出错。
Parameter(s):
参数:
In the first case, "ofEpochSecond(long sec_val)",
在第一种情况下,“ ofEpochSecond(long sec_val)”,
In the second case, "ofEpochSecond(long sec_val, long nanos_adjust)",
在第二种情况下,“ ofEpochSecond(long sec_val,long nanos_adjust)”,
Return value:
返回值:
In both the cases, the return type of the method is Instant,
在这两种情况下,方法的返回类型均为Instant 。
In the first cases, it returns the Instant that represent the given seconds.
在第一种情况下,它返回代表给定秒数的Instant。
In the second cases, it returns the Instant that represent the given seconds and nano fraction of seconds.
在第二种情况下,它返回表示给定秒数和秒的毫微秒数的Instant。
Example:
例:
// Java program to demonstrate the example // of ofEpochSecond() method of Instantimport java.time.*;import java.time.temporal.*;public class OfEpochSecondOfInstant { public static void main(String args[]) { long epoch_sec = 25; long nanos_adjust = 25000; // 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(); // Here, this method represents the given second // from the java epoch of 1970-01-01T00:00:00Z Instant of_epoch_sec = ins1.ofEpochSecond(epoch_sec); // Display of_epoch_sec System.out.println("ins1.ofEpochSecond(epoch_sec): " + of_epoch_sec); // Here, this method represents the instant by using // seconds and nanoseconds from the java epoch // of 1970-01-01T00:00:00Z of_epoch_sec = ins2.ofEpochSecond(epoch_sec, nanos_adjust); // Display of_epoch_sec System.out.println("ins2.ofEpochSecond(epoch_sec,nanos_adjust): " + of_epoch_sec); }}
Output
输出量
Instant ins1 and ins2: ins1: 2006-04-03T05:10:15Zins2: 2020-05-28T07:12:35.393208Zins1.ofEpochSecond(epoch_sec): 1970-01-01T00:00:25Zins2.ofEpochSecond(epoch_sec,nanos_adjust): 1970-01-01T00:00:25.000025Z
翻译自:
java 根据类名示例化类
转载地址:http://gixzd.baihongyu.com/