Java SimpleDateFormat Class

Introduction

SimpleDateFormat is a concrete subclass of DateFormat.

It can define the formatting patterns via its constructor.

SimpleDateFormat(String formatString ) 

The argument formatString describes how date and time information is displayed.

An example of its use is given here:

SimpleDateFormat sdf = SimpleDateFormat("dd MMM yyyy hh:mm:ss zzz"); 

The following table lists the symbols used in the formatting string.

Symbol Description
a AM or PM
d Day of month (1-31)
h Hour in AM/PM (1-12)
k Hour in day (1-24)
m Minute in hour (0-59)
s Second in minute (0-59)
u Day of week, with Monday being 1
w Week of year (1-52)
y Year
z Time zone
D Day of year (1-366)
E Day of week (for example, Thursday)
F Day of week in month
G Era (for example, AD or BC)
H Hour in day (0-23)
K Hour in AM/PM (0-11)
L Month
M Month
S Millisecond in second
W Week of month (1-5)
X Time zone in ISO 8601 format
Y Week year
Z Time zone in RFC 822 format
// Demonstrate SimpleDateFormat.
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
  public static void main(String args[]) {
    Date date = new Date();
    SimpleDateFormat sdf;/*from  w  w w.  j  a v  a 2s  . c o  m*/
    sdf = new SimpleDateFormat("hh:mm:ss");
    System.out.println(sdf.format(date));
    sdf = new SimpleDateFormat("dd MMM yyyy hh:mm:ss zzz");
    System.out.println(sdf.format(date));
    sdf = new SimpleDateFormat("E MMM dd yyyy");
    System.out.println(sdf.format(date));
  }
}



PreviousNext

Related