java 根据类名示例化类_Java即时类| EpochSecond()方法的示例

java 根据类名示例化类

EpochSecond()方法的即时类 (Instant Class ofEpochSecond() method)

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)”,

    • long sec_val – represents the number of seconds in value since 1970-01-01T00:00:00Z.
    • long sec_val –表示自1970-01-01T00:00:00Z以来的秒数。
  • In the second case, "ofEpochSecond(long sec_val, long nanos_adjust)",

    在第二种情况下,“ ofEpochSecond(long sec_val,long nanos_adjust)”,

    • long sec_val – Similar as defined in the first case.
    • long sec_val –与第一种情况中定义的类似。
    • long nanos_adjust – represents the adjustment when the second reaches out of range.
    • 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 Instant

import 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:15Z
ins2: 2020-05-28T07:12:35.393208Z

ins1.ofEpochSecond(epoch_sec): 1970-01-01T00:00:25Z
ins2.ofEpochSecond(epoch_sec,nanos_adjust): 1970-01-01T00:00:25.000025Z


翻译自: https://www.includehelp.com/java/instant-ofepochsecond-method-with-example.aspx

java 根据类名示例化类

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值