Bài 19 Java cơ bản: Thời gian hệ thống

Tóm tắt nội dung: Giới thiệu cách lấy thời gian hệ thống trong Java.
Nội dung:
Cách lấy milisecond:
Code:
public static void main(String[] args) {
            long start = System.currentTimeMillis();
            //Do something…
            long end = System.currentTimeMillis();
            System.out.println(start – end);
      }
Cách lấy nanosecond:
Code:
          public static void main(String[] args) {
            long start = System.nanoTime();
            //Do something…
            long end = System.nanoTime();
            System.out.println(start – end);
      }
Cách hiển thị ngày giờ trong hệ thống:
Code:
public static void main(String[] args) {
DateFormat dateFormat = new SimpleDateFormat (“yyyy-MM-dd HH:mm:ss”);//định dạng ngày giờ
            Date date = new Date();// lấy ngày giờ hệ thống
            String string = dateFormat.format(date);
            System.out.println(string);
      }
Các bạn xem thêm video để hiểu rõ hơn về thời gian hệ thống.
 
Nếu bạn có câu hỏi vui lòng để lại comment hoặc gởi câu hỏi theo địa chỉ: studyandsharevn@gmail.com. 

About Study and Share

Study and Share knowledge

Posted on October 18, 2011, in Java Basic Tutorials and tagged , , . Bookmark the permalink. 3 Comments.

  1. Anh Việt ơi cho em hỏi : cái lỗi này là lỗi gì anh ?
    Exception in thread “main” java.lang.IllegalArgumentException: Cannot format given Object as a Date
    at java.text.DateFormat.format(Unknown Source)
    at java.text.Format.format(Unknown Source)
    at JavaBasic.Date.main(Date.java:10)

  1. Pingback: 58 video hướng dẫn Java Basic chi tiết | Nesk BM

Leave a comment