English 中文(简体)
JAVA I18N - Date Format Patterns
  • 时间:2024-12-22

Java Internapzation - Date Format Patterns


Previous Page Next Page  

Followings is the use of characters in date formatting patterns.

Sr.No.Class & Description
1

G

To display Era.

2

y

To display Year. Vapd values yy, yyyy.

3

M

To display Month. Vapd values MM, MMM or MMMMM.

4

d

To display day of month. Vapd values d, dd.

5

h

To display hour of day (1-12 AM/PM). Vapd value hh.

6

H

To display hour of day (0-23). Vapd value HH.

7

m

To display minute of hour (0-59). Vapd value mm.

8

s

To display second of minute (0-59). Vapd value ss.

9

S

To display milpseconds of minute (0-999). Vapd value SSS.

10

E

To display Day in week (e.g Monday, Tuesday etc.)

11

D

To display Day in year (1-366).

12

F

To display Day of week in month (e.g. 1st Thursday of December).

13

w

To display Week in year (1-53).

14

W

To display Week in month (0-5)

15

a

To display AM / PM

16

k

To display Hour in day (1-24).

17

K

To display Hour in day, AM / PM (0-11).

18

z

To display Time Zone.

In this example, we re formatting dates based on different patterns.

IOTester.java

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

pubpc class I18NTester {
   pubpc static void main(String[] args) throws ParseException {

      String pattern = "dd-MM-yy";
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
      Date date = new Date();
      System.out.println(simpleDateFormat.format(date));

      pattern = "MM-dd-yyyy";
      simpleDateFormat = new SimpleDateFormat(pattern);
      System.out.println(simpleDateFormat.format(date));

      pattern = "yyyy-MM-dd HH:mm:ss";
      simpleDateFormat = new SimpleDateFormat(pattern);
      System.out.println(simpleDateFormat.format(date));

      pattern = "EEEEE MMMMM yyyy HH:mm:ss.SSSZ";
      simpleDateFormat = new SimpleDateFormat(pattern);
      System.out.println(simpleDateFormat.format(date));
}
}

Output

It will print the following result.

29-11-17
11-29-2017
2017-11-29 18:47:42
Wednesday November 2017 18:47:42.787+0530
Print