[Java] Take the time in Java – Time in Java
In programming much as we need to calculate the running time of the program or the duration of a task is to methodically handled more efficiently. Java gives us some time taking the following system:
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);
}
}



Recent Comments