How to get date, month, year values from the current date in Java

 Posted On  | Yashwant Chavan 

Sample Code to display the Current Date, Current Month , Current Year Value using java.util.Calendar abstract class.

Display Current Date, Month and Year Value Using Java

java.util.Calendar abstract class provide methods which return the date details. Below sample code Display the Current Date, Current Month and Current Year Values.

package com.net.technicalkeeda.bean;

import java.util.Calendar;

public class DateExample {

 public static void main(String[] args) {
 Calendar cal = Calendar.getInstance();
        int todaysDate = cal.get(Calendar.DATE);
        int currentMonth = cal.get(Calendar.MONTH) + 1;
        int currentYear = cal.get(Calendar.YEAR);
        
        System.out.println("Date:- " + todaysDate);
        System.out.println("Month:- " + currentMonth);
        System.out.println("Year:- " + currentYear);       

 }

}

Calendar.DATE

Field number for get and set indicating the day of the month. This is a synonym for DAY_OF_MONTH. The first day of the month has value 1.

Calendar.MONTH

Field number for get and set indicating the month. This is a calendar-specific value. The first month of the year in the Gregorian and Julian calendars is JANUARY which is 0; the last depends on the number of months in a year.

Calendar.MONTH

Field number for get and set indicating the year. This is a calendar-specific value; see subclass documentation.

Output

Date:- 8
Month:- 11
Year:- 2012

Finish :)

Hope this tutorial is helpful to you , In case if you have any questions please provide your comment below. Thanks


© technicalkeeda.com 2017

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0363 seconds.