Home Java Java-get-example Java program to get current date
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Java program to get current date

Advertisement
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 GetDateNow.java as follows:

GetDateNow.java

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)
Advertisement

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:

Output:

C:\javaexamples>javac GetDateNow.java

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

Download Source Code

Advertisements

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: October 24, 2008

Related Tutorials

Ask Questions?    Discuss: Java program to get current date now  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
gupta
June 9, 2011
java prog

plz giv the prog fr d folloein outpu 123454321 1234 4321 123 321 12 21 1 1
shoubhik
November 15, 2011
java

This is the best site in the world for beginner prorammers.
Vivek
November 21, 2011
Thanks

Thanks for help.
Puneet Verma
January 13, 2012
Getting Error

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

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 ??
Akash
February 25, 2012
Getting functions of java packages

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

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
java

why we are using object creation?
Jaques
June 25, 2012
CSE - JAVA

Thanks. That was very informative.
vivek
July 18, 2013
needed

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

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

it is good
DMCA.com