Java I18N – Date Format Patterns


Java Internationalization – Date Format Patterns


”;


Followings is the use of characters in date formatting patterns.

Sr.No. Class & Description
1

G

To display Era.

2

y

To display Year. Valid values yy, yyyy.

3

M

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

4

d

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

5

h

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

6

H

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

7

m

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

8

s

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

9

S

To display milliseconds of minute (0-999). Valid 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.

Example

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

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

public class I18NTester {
   public 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.

07-06-24
06-07-2024
2024-06-07 16:04:40
Friday June 2024 16:04:40.866+0530

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *