59. Formatting date and time

The previous problem contains some flavors of formatting date and time via SimpleDateFormat.format() and DateTimeFormatter.format(). In order to define format patterns, the developer must be aware of the format pattern syntax. In other words, the developer must be aware of the set of symbols that are used by the Java date-time API in order to recognize a valid format pattern.

Most of the symbols are common to SimpleDateFormat (before JDK 8) and to DateTimeFormatter (starting with JDK 8). The following table lists the most common symbols—the complete list is available in the JDK documentation:

Letter

Meaning

Presentation

Example

y

year

year

1994; 94

M

month of year

number/text

7; 07; Jul; July; J

W

week of month

number

4

E

day of week

text

Tue; Tuesday; T

d

day of month

number

15

H

hour of day

number

22

m

minute of hour

number

34

s

second of minute

number

55

S

fraction of second

number

345

z

time zone name

zone-name

Pacific Standard Time; PST

Z

zone offset

zone-offset

-0800

V

time zone id (JDK 8)

zone-id

America/Los_Angeles; Z; -08:30

 

Some format pattern examples are available in the following table:

Pattern

Example

yyyy-MM-dd

2019-02-24

MM-dd-yyyy

02-24-2019

MMM-dd-yyyy

Feb-24-2019

dd-MM-yy

24-02-19

dd.MM.yyyy

24.02.2019

yyyy-MM-dd HH:mm:ss

2019-02-24 11:26:26

yyyy-MM-dd HH:mm:ssSSS

2019-02-24 11:36:32743

yyyy-MM-dd HH:mm:ssZ

2019-02-24 11:40:35+0200

yyyy-MM-dd HH:mm:ss z

2019-02-24 11:45:03 EET

E MMM yyyy HH:mm:ss.SSSZ

Sun Feb 2019 11:46:32.393+0200

yyyy-MM-dd HH:mm:ss VV (JDK 8)

2019-02-24 11:45:41 Europe/Athens

Before JDK 8, a format pattern can be applied via SimpleDateFormat:

// yyyy-MM-dd
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String stringDate = formatter.format(date);

Starting with JDK 8, a format pattern can be applied via DateTimeFormatter:

  • For LocalDate (date without a time zone in the ISO-8601 calendar system):
// yyyy-MM-dd
LocalDate localDate = LocalDate.now();
DateTimeFormatter formatterLocalDate
= DateTimeFormatter.ofPattern("yyyy-MM-dd");
String stringLD = formatterLocalDate.format(localDate);

// or shortly
String stringLD = LocalDate.now()
.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
  • For LocalTime (time without a time zone in the ISO-8601 calendar system):
// HH:mm:ss
LocalTime localTime = LocalTime.now();
DateTimeFormatter formatterLocalTime
= DateTimeFormatter.ofPattern("HH:mm:ss");
String stringLT
= formatterLocalTime.format(localTime);

// or shortly
String stringLT = LocalTime.now()
.format(DateTimeFormatter.ofPattern("HH:mm:ss"));
  • For LocalDateTime (date-time without a time zone in the ISO-8601 calendar system):
// yyyy-MM-dd HH:mm:ss
LocalDateTime localDateTime = LocalDateTime.now();
DateTimeFormatter formatterLocalDateTime
= DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String stringLDT
= formatterLocalDateTime.format(localDateTime);

// or shortly
String stringLDT = LocalDateTime.now()
.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

  • For ZonedDateTime (date-time with a time zone in the ISO-8601 calendar system):
// E MMM yyyy HH:mm:ss.SSSZ
ZonedDateTime zonedDateTime = ZonedDateTime.now();
DateTimeFormatter formatterZonedDateTime
= DateTimeFormatter.ofPattern("E MMM yyyy HH:mm:ss.SSSZ");
String stringZDT
= formatterZonedDateTime.format(zonedDateTime);

// or shortly
String stringZDT = ZonedDateTime.now()
.format(DateTimeFormatter
.ofPattern("E MMM yyyy HH:mm:ss.SSSZ"));
  • For OffsetDateTime (date-time with an offset from UTC/GMT in the ISO-8601 calendar system):
// E MMM yyyy HH:mm:ss.SSSZ
OffsetDateTime offsetDateTime = OffsetDateTime.now();
DateTimeFormatter formatterOffsetDateTime
= DateTimeFormatter.ofPattern("E MMM yyyy HH:mm:ss.SSSZ");
String odt1 = formatterOffsetDateTime.format(offsetDateTime);

// or shortly
String odt2 = OffsetDateTime.now()
.format(DateTimeFormatter
.ofPattern("E MMM yyyy HH:mm:ss.SSSZ"));
  • For OffsetTime (time with an offset from UTC/GMT in the ISO-8601 calendar system):
// HH:mm:ss,Z
OffsetTime offsetTime = OffsetTime.now();
DateTimeFormatter formatterOffsetTime
= DateTimeFormatter.ofPattern("HH:mm:ss,Z");
String ot1 = formatterOffsetTime.format(offsetTime);

// or shortly
String ot2 = OffsetTime.now()
.format(DateTimeFormatter.ofPattern("HH:mm:ss,Z"));

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset