Java Calendar - Lấy giờ hiện tại theo định dạng (How to get time with format in Java).





Bài viết này sẽ hướng dẫn sử dụng lớp Calendar để lấy giờ hiện tại theo một số định dạng 12 giờ hoặc 24 giờ trong ngôn ngữ lập trình java.







getTimeWithFormatCurrent.java
/**
 * @(#)getTimeWithFormatCurrent.java
 *
 * getTimeWithFormatCurrent application
 *
 * @author BUI NGOC SON
 * @version 1.00 2014/2/4
 */

import java.util.Calendar;

public class getTimeWithFormatCurrent {
   
    public static void main(String[] args) {
       
        //Sử dụng phương thức '.getInstance' để khởi tạo đối tượng của lớp Calendar
        //Use '.getInstance' methos to create oject of lớp Calendar.
        Calendar cal = Calendar.getInstance();
       
        //Lấy thông tin giờ hiện tại.
        //Get current time information
        System.out.println("Current Hour in 12 hour format is : " + cal.get(Calendar.HOUR));
        System.out.println("Current Hour in 24 hour format is : " + cal.get(Calendar.HOUR_OF_DAY));
       
        //Lấy thông tin phút hiện tai.
        //Get current minute time.
        System.out.println("Current Minute is : " + cal.get(Calendar.MINUTE));
       
        //Lấy thông giây hiện tai.
        //get current second time.
        System.out.println("Current Second is : " + cal.get(Calendar.SECOND));
       
        //Lấy thông tin mili giây hiện tai.
        //Get current millisecond.
        System.out.println("Current Millisecond is : " + cal.get(Calendar.MILLISECOND));               

        System.out.println("\n \n \t -- vnlives.net --");
    }
}

Dưới đây là kết quả sau khi chạy ứng dụng.

















No comments:

Post a Comment