Home Java Java-get-example Get Date and Time

Related Tutorials


 
 

Share on Google+Share on Google+

Get Date and Time

Advertisement
In the example given below we will see, how to display current date and time in the user given format.

Get Date and Time

     

In the example given below we will see, how to display current date and time in the user given format. To do all this we have first created instance of the calendar class that returns object of the same class whose time fields have been initialized with the current date and time. getTime() method returns current time of date type.

 

 

 

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

public class DateAndTime {
  public static final String date = "yyyy-MM-dd HH:mm:ss";

  public static String now() {
  Calendar calendar = Calendar.getInstance();
  SimpleDateFormat dateFormat = new SimpleDateFormat(date);
  return dateFormat.format(calendar.getTime());
}
  public static void  main(String arg[]) {
  System.out.println("Date and Time : " + DateAndTime.now());
  }
}

Output will be displayed as:

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 22, 2008

Related Tutorials

Discuss: Get Date and Time  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Suruchi Bapna
May 19, 2013
request

u have to explain whole program as which function is use for what. Eg: Why we are using SimpleDateFormat or other code. please explain whole codes. I am requesting this for all codes in whole tutorials.
DMCA.com