java.time.Instant.from()方法

java.time.Instant.from(TemporalAccessor temporal)方法从时态对象获取Instant的实例。

声明

以下是java.time.Instant.from(TemporalAccessor temporal)方法的声明。

public static Instant from(TemporalAccessor temporal)

参数

  • temporal - 要转换的时间对象,而不是null

返回值

瞬间,不是null

异常

  • DateTimeException - 如果无法转换为Instant

示例

以下示例显示了java.time.Instant.from(TemporalAccessor temporal)方法的用法。

package com.zaixian;

import java.time.Instant;
import java.time.ZonedDateTime;

public class InstantDemo {
   public static void main(String[] args) {

      ZonedDateTime zonedDateTime = ZonedDateTime.now();

      Instant instant = Instant.from(zonedDateTime);

      System.out.println(instant);
   }
}

编译并运行上面的程序,这将产生以下结果 -

2017-03-10T09:29:03.044Z

上一篇: java.time.Instant类 下一篇: java.time.LocalDate类