Java Instant ofEpochMilli()方法及实例

Java Instant ofEpochMilli()方法及实例

Instant类ofEpochMilli() 方法有助于使用从1970-01-01T00:00:00Z的纪元中作为参数传递的毫秒来获得一个瞬间。这个返回的毫秒被用来通过转换获得不同的时间单位,如秒等。

语法

public static Instant 
    ofEpochMilli(long epochMilli)

参数: 该方法接受一个参数epochMilli,是来自1970-01-01T00:00:00Z的毫秒值。

返回: 该方法以毫秒为单位返回从大纪元开始的即时时间。

异常: 如果结果超过了最大或最小的瞬间,该方法会抛出DateTimeException。

下面的程序说明了ofEpochMilli()方法。

程序1 :

// Java program to demonstrate
// Instant.ofEpochMilli() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a long variable for milliseconds
        long milliseconds
            = 999999000;
  
        // get Instant using ofEpochMilli() method
        Instant instant
            = Instant.ofEpochMilli(milliseconds);
  
        // print result
        System.out.println("Instant: "
                           + instant);
    }
}

输出:

Instant: 1970-01-12T13:46:39Z

程序2

// Java program to demonstrate
// Instant.ofEpochMilli() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // get Instant using ofEpochMilli() method
        // passed epoch millisecond is 73264271044L
        Instant instant
            = Instant.ofEpochMilli(73264271044L);
  
        // print result
        System.out.println("Instant: "
                           + instant);
    }
}

输出:

Instant: 1972-04-27T23:11:11.044Z

参考文献: https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#ofEpochMilli(long)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程