Date and Time in Java

Date and Time in Java


Posted in : Core Java Posted on : September 29, 2010 at 6:17 PM Comments : [ 0 ]

This section contains the detail about the date and time in java.

Date and Time in Java

The 'java.util' package contains date class which contains current date and time.

Getting current Date and Time

For getting current date and time , you need to first initialize the object of class date. After creating it's object you can print it using 'tostring( )' method.

Example :


import java.util.Date;

public class DateDemo {
public static void main(String args[]) {
// Instantiate a Date object
Date date = new Date();

// display time and date using toString()
System.out.println(date.toString());
}
}

Output :

C:\Program Files\Java\jdk1.6.0_18\bin>javac DateDemo .java

C:\Program Files\Java\jdk1.6.0_18\bin>java DateDemo
Tue Sep 28 17:52:01 IST 2010

Date Formatting using SimpleDateFormat

The SimpleDateFormat is a class for format and parse date in a locale-sensitive manner. Any user can use it's predefine character codes to create it's own pattern for formatting date-time as follows :

Example :

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

class DateDemo {
   public static void main(String args[]) {

       Date dNow = new Date( );
       SimpleDateFormat ft = 
       new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");

       System.out.println("Current Date: " + ft.format(dNow));
   }
}

Output :

C:\Program Files\Java\jdk1.6.0_18\bin>javac DateDemo .java

C:\Program Files\Java\jdk1.6.0_18\bin>java DateDemo
Sun 2010.09.28 at 06:14:09 PM PDT

In this code :

'E' means - Day in week

'y', 'm' & 'd' means - year, month & date respectively.

'h', 'm', 's' means - hour, minutes & second respectively.

'a' means -  A.M./P.M. marker.

'z' means - Time zone.

Date Formatting using printf :

We can also format date and time using printf. In this approach, we are using a two letter format starting with t and ending in one of the letters of the table given below. For example :

import java.util.Date;

class DateDemoBasic {
public static void main(String args[]) {
// Instantiate a Date object
Date date = new Date();

// display time and date using toString()
System.out.printf("%tc", "Current Time : ", date);
}
}

Output :

C:\Program Files\Java\jdk1.6.0_18\bin>javac DateDemoBasic.java

C:\Program Files\Java\jdk1.6.0_18\bin>java DateDemoBasic
 Due date: September 29, 2010

For formatting separate parts, it also has separate two letter codes which is also given below table. Given below the program for separate formatting :


import java.util.Date;

class DateDemo {
public static void main(String args[]) {
// Instantiate a Date object
Date date = new Date();

// display time and date using toString()
System.out.printf("%1$s %2$tB %2$td, %2$tY", 
"Due date:", date);
}

Output :

C:\Program Files\Java\jdk1.6.0_18\bin>javac DateDemo .java

C:\Program Files\Java\jdk1.6.0_18\bin>java DateDemo
Due date: September 29, 2010

Date and Time Conversion Characters :

Character Description Example
c Complete date and time Mon May 04 09:51:52 CDT 2009
F ISO 8601 date 2004-02-09
D U.S. formatted date (month/day/year) 02/09/2004
T 24-hour time 18:05:19
r 12-hour time 06:05:19 pm
R 24-hour time, no seconds 18:05
Y Four-digit year (with leading zeroes) 2004
y Last two digits of the year (with leading zeroes) 04
C First two digits of the year (with leading zeroes) 20
B Full month name February
b Abbreviated month name Feb
n Two-digit month (with leading zeroes) 02
d Two-digit day (with leading zeroes) 03
e Two-digit day (without leading zeroes) 9
A Full weekday name Monday
a Abbreviated weekday name Mon
j Three-digit day of year (with leading zeroes) 069
H Two-digit hour (with leading zeroes), between 00 and 23 18
k Two-digit hour (without leading zeroes), between 0 and 23 18
I Two-digit hour (with leading zeroes), between 01 and 12 06
l Two-digit hour (without leading zeroes), between 1 and 12 6
M Two-digit minutes (with leading zeroes) 05
S Two-digit seconds (with leading zeroes) 19
L Three-digit milliseconds (with leading zeroes) 047
N Nine-digit nanoseconds (with leading zeroes) 047000000
P Uppercase morning or afternoon marker PM
p Lowercase morning or afternoon marker pm
z RFC 822 numeric offset from GMT -0800
Z Time zone PST
s Seconds since 1970-01-01 00:00:00 GMT 1078884319
Q Milliseconds since 1970-01-01 00:00:00 GMT 1078884319047

There are other useful classes related to Date and time. For more detail you can refer to Java Standard documentation.

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics