Friday, August 28, 2009

Getting the current date using Java

Calendar.MONTH:
This field of the Calendar class returns the static integer which is the value of the month of the year.

Calendar.DAY:
This field returns a static integer value which is the value of the day of the month in the year.

Calendar.YEAR:
This field returns a static integer value which is the current year.

Here is the code of the program:

package com.anuj.utils;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class CurrentDate {
    /**
     * @param args
     */
    public static void main(String[] args) {
        Calendar cal = new GregorianCalendar();
        int month = cal.get(Calendar.MONTH);
        int year = cal.get(Calendar.YEAR);
        int day = cal.get(Calendar.DAY_OF_MONTH);
        System.out.println("Current date : " + day + "/" + (month + 1) + "/"
                + year);
    }

}

Author : Anuj Patel
Blog : http://goldenpackagebyanuj.blogspot.in/