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

java 根据类名示例化类

即时类from()方法 (Instant Class from() method)

  • from() method is available in java.time package.

    from()方法在java.time包中可用。

  • from() method is used to return a copy of the Instant from the given TemporalAccessor object.

    from()方法用于从给定的TemporalAccessor对象返回Instant的副本。

  • from() method is a static method, it is accessible with the class name and if we try to access the method with the class object then we will not get an error.

    from()方法是一个静态方法,可以使用类名进行访问,如果尝试使用类对象访问该方法,则不会收到错误。

  • from() method may throw an exception at the time of returning Instant from the given temp_acc.

    给定的temp_acc返回Instant时, from()方法可能会引发异常。

    DateTimeException: This exception may throw when the given templ_acc couldn't convert into Instant.

    DateTimeException :当给定的templ_acc无法转换为Instant时,可能抛出此异常。

Syntax:

句法:

    public static Instant from(TemporalAccessor temp_acc);

Parameter(s):

参数:

  • TemporalAccessor temp_acc – represents the temporal accessor to convert to Instant.

    TemporalAccessor temp_acc –表示要转换为Instant的临时访问器。

Return value:

返回值:

The return type of this method is Instant, it returns the Instant that the holds the same value from the given object.

此方法的返回类型为Instant ,它返回Instant,该Instant具有给定对象的相同值。

Example:

例:

// Java program to demonstrate the example 
// of Instant from(TemporalAccessor temp_acc)
// method of Instant

import java.time.*;
public class FromOfInstant {
    public static void main(String args[]) {
        // Instantiates a OffsetDateTime 
        // and a ZonedDateTime object
        OffsetDateTime off_da_time = OffsetDateTime.now();
        ZonedDateTime zo_da_time = ZonedDateTime.parse("2008-07-06T20:25:30.213+03:00[Africa/Asmara]");

        // Display off_da_time
        // and zo_da_time
        System.out.println("OffsetDateTime off_da_time: ");
        System.out.println("off_da_time: " + off_da_time);
        System.out.println();
        System.out.println("ZonedDateTime zo_da_time: ");
        System.out.println("zo_da_time: " + zo_da_time);

        System.out.println();

        // Here, this method creates an Instant
        // from the given TemporalAccessor i.e.
        // here we are creating an Instant(ins)
        // from the given OffsetDateTime(off_da_time)
        Instant inst = Instant.from(off_da_time);

        // Display inst
        System.out.println("Instant.from(off_da_time): " + inst);

        // Here, this method creates an Instant
        // from the given TemporalAccessor i.e.
        // here we are creating an Instant(ins)
        // from the given ZonedDateTime(zo_da_time)
        inst = Instant.from(zo_da_time);

        // Display inst
        System.out.println("Instant.from(zo_da_time): " + inst);
    }
}

Output

输出量

OffsetDateTime off_da_time: 
off_da_time: 2020-05-21T22:00:46.891244Z

ZonedDateTime zo_da_time: 
zo_da_time: 2008-07-06T20:25:30.213+03:00[Africa/Asmara]

Instant.from(off_da_time): 2020-05-21T22:00:46.891244Z
Instant.from(zo_da_time): 2008-07-06T17:25:30.213Z


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

java 根据类名示例化类

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值