Home Java Java-get-example Get Date and Time



Get Date and Time
Posted on: October 22, 2008 at 12:00 AM
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

Related Tags for Get Date and Time:
cclassormdateformtimeobjectcalendarmethoduserformatgettypedisplayfieldreturnfieldsinstancethiscreateforieexamplewithtoiniexamldeilitliinitsplusepeinitializeimfirstcecurrentinrmcalasstamntplayoscaosjispndaclendallmehowobjdosamexaxampsurnspcreatedeeatisirhaimellivmplgettimeeaandarsattzssamssamrenthavbelosthatdispldsinitialjepleplndo


More Tutorials from this section

Ask Questions?    Discuss: Get Date and Time  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.