Home Java Java-get-example Get Time And Date
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Get Time And Date

Advertisement
The Class Date provides you a specific instant in time( millisecond) precision. This class enabled you to interprets allowed dates as year, month, day, hour, minute, and second values and date formatting and parsing of date string.

Get Time And Date

     

The Class  Date provides you  a specific instant in time( millisecond) precision. This class enabled you to interprets  allowed  dates as year, month, day, hour, minute, and second values and date formatting and parsing of date string.

Understand with Example

In this Tutorial we want to describe you a code that helps you in understanding in a Getting Time and Date. For this we have a class 'GetTimeAndDate'.Inside the main method we declared a string variable, formatter and instantiate a date class.

1)Date ( )   -   This is used to instantiate the date class and allocate the memory to a date object, such that it represent the time allocated to it in the nearest millisecond.

2)SimpleDateFormat- is a  formatting concrete class that helps you in parsing date in a locale-sensitive manner. This allows you to format (date -> text), parsing (text -> date), and normalization.

3)format(date) -This method return you a formatted object to a string and stored in a string variable s.

Finally the System.out .println print the Date, Time and date-time altogether.

GetTimeAndDate.java

import java.util.*;
import java.text.*;

public class GetTimeAndDate {

  public static void main(String args[]) {

  String s;
  Format formatter;
  Date date = new Date();

  formatter = new SimpleDateFormat("dd/MMM/yyyy");
  s = formatter.format(date);
  System.out.println("Date : " + s);

  formatter = new SimpleDateFormat("hh:mm:ss a");
  s = formatter.format(date);
  System.out.println("Time : " + s);

  formatter = new SimpleDateFormat("dd MMMM yyyy HH:mm:ss");
  s = formatter.format(date);
  System.out.println("Time : " + s);
  }
}

Output
Date : 03/Nov/2008
Time : 04:28:52 PM
Time : 03 November 2008 16:28:52
Download 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: November 5, 2008

Related Tutorials

Ask Questions?    Discuss: Get Time And Date   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com