Sunday 22 July 2012

How to Format Dates and Times using SimpleDateFormat in Java

The SimpleDateFormat lets you build custom date formats.A pattern of special characters is used to specify the format of the date.For example, dd/mm/yyyy, mm/dd/yyyy, yyyy-mm-dd, and so on.For a complete listing, see the table given below.

Date and Time Patterns


Date and time formats are specified by date and time pattern strings. Within date and time pattern strings, unquoted letters from 'A' to 'Z' and from 'a' to 'z' are interpreted as pattern letters representing the components of a date or time string. Text can be quoted using single quotes (') to avoid interpretation.All other characters are not interpreted; they're simply copied into the output string during formatting or matched against the input string during parsing.

The following pattern letters are defined (all other characters from 'A' to 'Z' and from 'a' to 'z' are reserved):



Symbol Meaning Type Example
G Era Text “GG” -> “AD”
y Year Number “yy” -> “03″
“yyyy” -> “2003″
M Month Text or Number “M” -> “7″
“M” -> “12″
“MM” -> “07″
“MMM” -> “Jul”
“MMMM” -> “December”
d Day in month Number “d” -> “3″
“dd” -> “03″
h Hour (1-12, AM/PM) Number “h” -> “3″
“hh” -> “03″
H Hour (0-23) Number “H” -> “15″
“HH” -> “15″
k Hour (1-24) Number “k” -> “3″
“kk” -> “03″
K Hour (0-11 AM/PM) Number “K” -> “15″
“KK” -> “15″
m Minute Number “m” -> “7″
“m” -> “15″
“mm” -> “15″
s Second Number “s” -> “15″
“ss” -> “15″
S Millisecond (0-999) Number “SSS” -> “007″
E Day in week Text “EEE” -> “Tue”
“EEEE” -> “Tuesday”
D Day in year (1-365 or 1-364) Number “D” -> “65″
“DDD” -> “065″
F Day of week in month (1-5) Number “F” -> “1″
w Week in year (1-53) Number “w” -> “7″
W Week in month (1-5) Number “W” -> “3″
a AM/PM Text “a” -> “AM”
“aa” -> “AM”
z Time zone Text “z” -> “EST”
“zzz” -> “EST”
“zzzz” -> “Eastern Standard Time”
Excape for text Delimiter “‘hour’ h” -> “hour 9″
Single quote Literal “ss”SSS” -> “45’876″



For Example:


import java.text.SimpleDateFormat;
import java.util.Date;

public class DateFormat {

 public static void main(String[] args) {

  Date dNow = new Date();
  SimpleDateFormat ft = null;

  ft = new SimpleDateFormat("dd/MM/yyyy");
  System.out.println("Current Date(dd/MM/yyyy): " + ft.format(dNow));

  ft = new SimpleDateFormat("dd-MM-yyyy");
  System.out.println("Current Date(dd-MM-yyyy): " + ft.format(dNow));

  ft = new SimpleDateFormat("dd-MMM-yy");
  System.out.println("Current Date(dd-MMM-yy): " + ft.format(dNow));

  ft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  System.out.println("Current Date(yyyy-MM-dd hh:mm:ss): "
    + ft.format(dNow));

  ft = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z");
  System.out.println("Current Date(E, dd MMM yyyy HH:mm:ss Z): "
    + ft.format(dNow));
 }

}



The output is


Current Date(dd/MM/yyyy): 22/07/2012
Current Date(dd-MM-yyyy): 22-07-2012
Current Date(dd-MMM-yy): 22-Jul-12
Current Date(yyyy-MM-dd hh:mm:ss): 2012-07-22 09:30:14
Current Date(E, dd MMM yyyy HH:mm:ss Z): Sun, 22 Jul 2012 21:30:14 +0530

No comments:

Post a Comment