[ジャワ] Javaで時間をかけて – Javaでの時間
プログラミングでは、治療は、より効果的な取得するために、プログラムの実行時間または特定のジョブの実行時間を計算する必要がある限り. Javaは、次のようにシステムを取って私たちにいくつかの時間を与えます:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
class TimeInJava{
public static void main(String[] args){
long start, end;
start = System.nanoTime(); // start lấy thời gian theo nanosecond
for (int i=0; i<100; i++);
end = System.nanoTime(); // start lấy thời gian theo nanosecond
System.out.println("Time Nano: " + (end - start));
start = System.currentTimeMillis(); // start lấy thời gian theo millisecond
for (long i=0; i<100000000; i++); //vòng lặp không thực hiện thêm lệnh nào
end = System.currentTimeMillis(); // start lấy thời gian theo millisecond
System.out.println("Time Millis: " + (end - start));
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// tạo 1 đối tượng có định dạng thời gian yyyy-MM-dd HH:mm:ss
Date date = new Date(); // lấy thời gian hệ thống
String stringDate = dateFormat.format(date);//Định dạng thời gian theo trên
System.out.println("Date: " + stringDate);
}
}



最近のコメント