Search This Blog

Tuesday, March 27, 2018

Lấy thời gian trong Java – Time in Java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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);
    }
}

No comments:

Post a Comment