Java Calendar - Lấy giá trị ngày, tháng, năm hiện tai. (How to get day, month, year current in java?)






Bài viết này sẽ cung cấp một đoạn mã dùng để lấy giá trị ngày, tháng, năm hiện tại trên computer bằng lớp(class) Calendar trong ngôn ngữ lập trình Java.







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

import java.util.Calendar;

public class getCurrentDateMonthYearYear {
   
    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 ngày hiện tai.
        //Get current date.
        System.out.println("Current Date is : " + cal.get(Calendar.DATE));       

        //Lấy tháng hiện tại, lưu ý trong java tháng bằng đầu từ 0 tới 11.
        //Get current month, month start from 0 to 11
        System.out.println("Current Month is : " + (cal.get(Calendar.MONTH) + 1 ));

        //Lấy năm hiện tai.           
        //Get current year.           
        System.out.println("Current Year is : " + cal.get(Calendar.YEAR));
       
        System.out.println("\n \n \t -- vnlives.net --");

    }
   
}

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



















2 comments:

  1. Thế sử dụng nó để so sánh thì làm như nào bác ơi?

    ReplyDelete
    Replies
    1. http://blog.vnlives.net/2014/04/how-to-compare-dates-with-before-after-in-java.html
      http://blog.vnlives.net/2014/04/How-to-compare-dates-with-compareTo-method-in-Java.html

      Delete