使用 Java 获取经过时间的分钟数


要获取某个操作在 Java 中经过的时间(以分钟为单位),我们使用 System.currentTimeMillis() 方法。java.lang.System.currentTimeMillis() 返回当前时间(以毫秒为单位)。

声明 −java.lang.System.currentTimeMillis() 声明如下 −

public static long currentTimeMillis()

该方法返回当前时间和 1970 年 1 月 1 日午夜(UTC 或纪元时间)之间的毫秒时间差。

下面我们来看一个在 Java 中计算某个操作经过时间的程序(以分钟为单位) −

示例

 在线演示

public class Example {
   public static void main(String[] args) throws Exception {
      // finding the time before the operation is executed
      long start = System.currentTimeMillis();
      for (int i = 0; i <5; i++) {
         Thread.sleep(60);
      }
      // finding the time after the operation is executed
      long end = System.currentTimeMillis();
      // finding the time difference
      float msec = end - start;
      // converting it into seconds
      float sec= msec/1000F;
      // converting it into minutes
      float minutes=sec/60F;
      System.out.println(minutes + " minutes");
   }
}

输出

0.0050000004 minutes

更新时间:2020 年 6 月 26 日

2K+ 浏览量

开启你的 职业生涯

通过完成课程取得认证

开始
广告