Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Example Java Util Getting the current date

Related Tutorials


 
 

Share on Google+Share on Google+

Getting the current date

Advertisement
This section shows the way of getting the current date of the system.

Getting the current date

     

This section shows the way of getting  the current date of the system.

In this section, through the given program you can get the current date in the proper format (Day/Month/Year). This program shows the current date by using the various methods and fields of the Calendar class. These are explained as follows:

Code Description:

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:

import java.util.*;

public class CurrentDate{
  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);
  }
}

Download this example.

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 16, 2007

Related Tutorials

Discuss: Getting the current date   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:3
MK
October 13, 2011
inset date time

how to store current date and time in mysql in jsp
karthik
December 8, 2011
need another solution

how to get date in parsed fields,like date,month,year
tigabu
April 13, 2013
java programing

to help me with the program of a date class
DMCA.com