使用 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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP