Java program to get current date

In this example program you will learn how to get the current date and time in Java program.

Java program to get current date now


In this example program we have to get the current date at present now. We have used Calendar class for getting the current date and time instance. For printing date in the specific format we have used SimpleDateFormat class of "java.text" package.

Calendar currentDate = Calendar.getInstance(); gets current date instance. To show this in the specific format we can use SimpleDateFormat class.

SimpleDateFormat formatter=  new SimpleDateFormat("yyyy/MMM/dd HH:mm:ss");
String dateNow = formatter.format(currentDate.getTime());

Above lines of code gets the current date and time in the yyyy/MMM/dd HH:mm:ss format.

Here is the full example code of as follows:

import java.util.Calendar;
import java.text.SimpleDateFormat;

public class GetDateNow {
  public static void  main(String arg[]) {
  Calendar currentDate = Calendar.getInstance();
  SimpleDateFormat formatter= 
new SimpleDateFormat("yyyy/MMM/dd HH:mm:ss");
  String dateNow = formatter.format(currentDate.getTime());
  System.out.println("Now the date is :=>  " + dateNow);

In this above program we have used the Calendar.getInstance() method of the Calendar class. This method is used to get the calendar instance using the default specified time zone and the specified locale. You can also specify the time zone and the locale while getting the instance of the calendar.

Here is the Declaration of the Calendar.getInstance()  method:

public static Calendar getInstance(TimeZone zone,Locale locale)

Parameters of the Calendar.getInstance()  method:

  • zone -- Here we specify the the time zone for the calendar data
  • locale -- Here we have to specify the locale for the calendar data

In the above program we have also used the SimpleDateFormat class for format the date. If you run the program you will get the following output:



C:\javaexamples>java GetDateNow
Now the date is :=> 2008/Oct/18 13:14:56

Download Source Code

Share on Google+Share on Google+

Java program to get current date

Posted on: October 24, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Java program to get current date now  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
Puneet Verma
January 13, 2012
Getting Error

String dateNow = formatter.format(currentDate.getTime()); no suitable mathod found for java.util.Date
February 25, 2012

thanks for this.. it works awesome but.. can we also have a running time in this ? i.e...seconds, minutes & hours all are in motion ??
February 25, 2012
Getting functions of java packages

how can we find in cmd, function of the packages to use
March 21, 2012

hello sir/madam, I have one problem please help me anyone I have hard code date in my java code for 1 min to 8 hours with 6 currency pairs like java.sql.Timestamp timestamp = Timestamp.valueOf(“2012-02-24 20:00:00?); then its working fine. If i have add for live chart like java.sql.Timestamp timestamp = new Timestamp(System.currentTimeMillis()); or java.util.Date date= new java.util.Date(); java.sql.Timestamp timestamp = new Timestamp(today.getTime()); then its taking lot of time to plot the jfreechart graph . so give me some suggestion or any commands need to add in my java code. Its urgent please.
G.Sarath chandranath
April 15, 2012

why we are using object creation?
June 25, 2012

Thanks. That was very informative.
July 18, 2013

How to use the JFormattedfield in java program
December 3, 2013
algorithm and flowchart

i want to read more about them
surjeet singh
September 19, 2011

it is good