Java 中的时间函数


Java 提供了 java.util 包中的 Date 类,该类封装了当前日期和时间。时间函数可通过 java.util.Date 类进行访问。这表示精度为毫秒的时间实例。

Java 中的一个时间函数是 getTime() 函数。它返回自 1970 年 1 月 1 日 00:00:00 GMT 以来经过的毫秒数。演示它的程序如下 −

示例

 实时演示

import java.util.*;
public class Example {
   public static void main(String[] args) {
      Date d = new Date(95, 7, 15);
      long num = d.getTime();
      System.out.println("From the date 1-1-1970 to the date 15-07-1995, " + num + "       milliseconds have passed.");
   }
}

输出

From the date 1-1-1970 to the date 15-07-1995, 808444800000 milliseconds have passed.

现在让我们来了解一下上面的程序。

首先,创建一个日期。然后使用函数 getTime() 来查找自 1970 年 1 月 1 日 00:00:00 GMT 以来经过的毫秒数。然后显示它。演示它的代码段如下 −

Date d = new Date(95, 7, 15);
long num = d.getTime();
System.out.println("From the date 1-1-1970 to the date 15-07-1995, " + num + " milliseconds have passed.");

更新于: 26-06-2020

735 次浏览

开始你的 职业生涯

通过完成课程获得认证

开始学习
广告
© . All rights reserved.