Java - Formatting Symbols for SimpleDateFormat

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

The following code shows how to embed your own message in the format.

Demo

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

public class Main {
  public static void main(String[] args) {
    // Create a GregorianCalendar object with September 19, 2028 as date
    GregorianCalendar gc = new GregorianCalendar(2028, Calendar.SEPTEMBER, 19);

    // Get date object
    Date birthDate = gc.getTime();

    // Create the pattern. You must place literals inside single quotes
    String pattern = "'I was born on the day' 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));

  }//  ww  w.  java2s. com
}

Result

Related Topic