Java DateTimeFormatter
last modified July 8, 2023
In this article we show how to formate and parse datetime values in Java with DateTimeFormatter.
DateTimeFormatter
class is used to format and parse modern Java API
datetime values.
The DateTimeFormatter
contains two basics methods:
format
and parse
.
DateTimeFormatter common constants
DateTimeFormatter
contains several common formatter constants such
as BASIC_ISO_DATE
or ISO_DATE_TIME
.
package com.zetcode; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class FormatterEx { public static void main(String[] args) { var now = LocalDateTime.now(); DateTimeFormatter dtf1 = DateTimeFormatter.BASIC_ISO_DATE; DateTimeFormatter dtf2 = DateTimeFormatter.ISO_DATE; DateTimeFormatter dtf3 = DateTimeFormatter.ISO_DATE_TIME; DateTimeFormatter dtf4 = DateTimeFormatter.ISO_WEEK_DATE; DateTimeFormatter dtf5 = DateTimeFormatter.ISO_ORDINAL_DATE; System.out.println(dtf1.format(now)); System.out.println(dtf2.format(now)); System.out.println(dtf3.format(now)); System.out.println(dtf4.format(now)); System.out.println(dtf5.format(now)); } }
The program formats the current datetime with five common formatter constants.
20221026 2022-10-26 2022-10-26T20:33:56.705430917 2022-W43-3 2022-299
DateTimeFormatter Instant
The Instant
is an instantaneous point on the time-line. We format
it with DateTimeFormatter.ISO_INSTANT
.
package com.zetcode; import java.time.Instant; import java.time.format.DateTimeFormatter; public class FormatterEx { public static void main(String[] args) { var now = Instant.now(); var dtf = DateTimeFormatter.ISO_INSTANT; System.out.println(dtf.format(now)); } }
The program formats the current Instant
value.
2022-10-26T18:42:14.117403659Z
DateTimeFormatter FormatStyle
We can format LocalDate
values with FormatStyle.SHORT
,
FormatStyle.MEDIUM
, FormatStyle.LONG
,
FormatStyle.FULL
format styles.
package com.zetcode; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.time.format.FormatStyle; public class FormatterEx { public static void main(String[] args) { var now = LocalDate.now(); var dtf1 = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT); var dtf2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM); var dtf3 = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG); var dtf4 = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL); System.out.println(dtf1.format(now)); System.out.println(dtf2.format(now)); System.out.println(dtf3.format(now)); System.out.println(dtf4.format(now)); } }
The program formats the current local date with the four format styles.
10/26/22 Oct 26, 2022 October 26, 2022 Wednesday, October 26, 2022
DateTimeFormatter.ofPattern
Custom formats can be defined with DateTimeFormatter.ofPattern
.
package com.zetcode; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class FormatterEx { public static void main(String[] args) { var now = LocalDateTime.now(); var dtf1 = DateTimeFormatter.ofPattern("yyyy-MM-dd"); var dtf2 = DateTimeFormatter.ofPattern("E, MMM dd yyyy"); var dtf3 = DateTimeFormatter.ofPattern("EEEE, MMM dd, yyyy HH:mm:ss a"); System.out.println(dtf1.format(now)); System.out.println(dtf2.format(now)); System.out.println(dtf3.format(now)); } }
The program prints the current local datetime with three custom formats.
2022-10-26 Wed, Oct 26 2022 Wednesday, Oct 26, 2022 21:01:49 PM
DateTimeFormatter localized custom formats
The next example creates localized datetime formats with ofPattern
and Locale
.
package com.zetcode; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Locale; public class FormatterEx { public static void main(String[] args) { var now = LocalDateTime.now(); String pattern = "EEEE, MMM dd, yyyy HH:mm:ss a"; var dtf1 = DateTimeFormatter.ofPattern(pattern, Locale.CHINA); var dtf2 = DateTimeFormatter.ofPattern(pattern, new Locale("RU", "ru")); var dtf3 = DateTimeFormatter.ofPattern(pattern, new Locale("SK", "sk")); System.out.println(dtf1.format(now)); System.out.println(dtf2.format(now)); System.out.println(dtf3.format(now)); } }
The program prints the current local datetime in Chinese, Russian, and Slovak.
星期三, 10月 26, 2022 21:07:17 下午 среда, окт. 26, 2022 21:07:17 PM streda, okt 26, 2022 21:07:17 PM
Java DateTimeFormatterBuilder
Complex formatters can be generated with DateTimeFormatterBuilder
.
package com.zetcode; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; import java.time.temporal.ChronoField; import java.util.Locale; public class FormatterEx { public static void main(String[] args) { var now = LocalDateTime.now(); DateTimeFormatter dtf = new DateTimeFormatterBuilder() .appendText(ChronoField.DAY_OF_MONTH) .appendLiteral(". ") .appendText(ChronoField.MONTH_OF_YEAR) .appendLiteral(" ") .appendText(ChronoField.YEAR) .parseCaseInsensitive() .toFormatter(new Locale("SK", "sk")); System.out.println(dtf.format(now)); } }
The program generates a formatter with DateTimeFormatterBuilder
.
26. októbra 2022
DateTimeFormatter parse
The parse
method fully parses the text producing a temporal object.
package com.zetcode; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.time.temporal.TemporalAccessor; public class FormatterEx { public static void main(String[] args) { var d = "2022-10-26"; DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE; TemporalAccessor ta = dtf.parse(d); var ld = LocalDate.from(ta); System.out.println(ld); } }
The program parses a string to produce a LocalDate
value.
In this article we have used DateTimeFormatter
to format and parse
datetime values.
Author
List all Java tutorials.