Java SimpleDateFormat formatting Symbols

Introduction

The following table is the List of Formatting Symbols for Formatting Date and Time

Letter
Date or Time Component
Presentation
Examples
G
Era designator
Text
AD
y
Year
Year
2018; 03
Y
Week-based year
Year
2018; 03
M
Month in year
Month
March; Mar; 03
w
Week in year
Number
27
W
Week in month
Number
2
D
Day in year
Number
189
d
Day in month
Number
10
F
Day of week in month
Number
2
E
Day in week
Text
Tuesday; Tue
a
AM/PM marker
Text
PM
H
Hour in day (0-23)
Number
0
k
Hour in day (1-24)
Number
24
K
Hour in AM/PM (0-11)
Number
0
h
Hour in AM/PM (1-12)
Number
12
m
Minute in hour
Number
30
s
Second in minute
Number
55
S
Millisecond
Number
978
z
Time zone
General time zone
Pacific Standard Time; PST; GMT-08:00
Z
Time zone
RFC 822 time zone
-0800
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
  public static void main(String[] args) {
    Date birthDate = new Date();

    // Create the pattern. You must place literals inside single quotes
    String pattern = "'date is' dd 'of the month' MMMM 'in' yyyy";

    // Create simple date format
    SimpleDateFormat simpleFormatter = new SimpleDateFormat(pattern);

    // Print the date
    System.out.println(simpleFormatter.format(birthDate));

  }//from w w w  .  ja v  a 2 s .  c om
}
import java.text.Format;
import java.text.SimpleDateFormat;

public class Main {
  public static void main(String[] argv) throws Exception {
    Format formatter;//  w ww  .ja  va2s.  c  o m

    // The year
    formatter = new SimpleDateFormat("yy"); // 02
    formatter = new SimpleDateFormat("yyyy"); // 2002

    // The month
    formatter = new SimpleDateFormat("M"); // 1
    formatter = new SimpleDateFormat("MM"); // 01
    formatter = new SimpleDateFormat("MMM"); // Jan
    formatter = new SimpleDateFormat("MMMM"); // January

    // The day
    formatter = new SimpleDateFormat("d"); // 9
    formatter = new SimpleDateFormat("dd"); // 09

    // The day in week
    formatter = new SimpleDateFormat("E"); // Wed
    formatter = new SimpleDateFormat("EEEE"); // Wednesday

    // The hour (1-12)
    formatter = new SimpleDateFormat("h"); // 8
    formatter = new SimpleDateFormat("hh"); // 08

    // The hour (0-23)
    formatter = new SimpleDateFormat("H"); // 8
    formatter = new SimpleDateFormat("HH"); // 08

    // The minutes
    formatter = new SimpleDateFormat("m"); // 7
    formatter = new SimpleDateFormat("mm"); // 07

    // The seconds
    formatter = new SimpleDateFormat("s"); // 3
    formatter = new SimpleDateFormat("ss"); // 03

    // The am/pm marker
    formatter = new SimpleDateFormat("a"); // AM

    // The time zone
    formatter = new SimpleDateFormat("z"); // PST
    formatter = new SimpleDateFormat("zzzz"); // Pacific Standard Time
    formatter = new SimpleDateFormat("Z"); // -0800
  }

}



PreviousNext

Related