DateTimeFormatter
layout: default
title: DateTimeFormatter
Formatting Dates and Times¶
LocalDate and LocalTime both override the toString() method, so presentation is easy.
LocalDate today = LocalDate.now();
System.out.println(today);
// "2018-02-06"
java.time.format.DateTimeFormatter to customize the output.
- Pass a format string to its static
ofPatternmethod to create a formatter object.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
System.out.println(formatter.format(today));
// "02/06/2018"
DateTimeFormatter provides several prebuilt formatter objects as public fields.
System.out.println(DateTimeFormatter.BASIC_ISO_DATE.format(today));
// "20180206"
System.out.println(DateTimeFormatter.ISO_DATE.format(today));
// "2018-02-06:
System.out.println(DateTimeFormatter.ISO_WEEK_DATE.format(today));
// "2018-W06-2"
System.out.println(DateTimeFormatter.ISO_ORDINAL_DATE.format(today));
// "2018-037"
LocalDateTime rightNow = LocalDateTime.now();
System.out.println(DateTimeFormatter.ISO_DATE.format(rightNow));
// "2018-02-06"
System.out.println(DateTimeFormatter.ISO_DATE_TIME.format(rightNow));
// "2018-02-06T17:11:01.666"
A formatter can only format data that's available in the temporal object.
LocalDate todaysDate = LocalDate.now();
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("MMM dd, yyyy HH:mm");
// System.out.println(formatter2.format(todaysDate));
// Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: HourOfDay
LocalDateTime currentDT = LocalDateTime.now();
System.out.println(DateTimeFormatter.ISO_ZONED_DATE_TIME.format(currentDT));
// Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: OffsetSeconds
Parsing Dates and Times¶
You can use DateTimeFormatter to parse a String that's in a non-ISO format to a LocalDate, LocalTime, LocalDateTime, or ZonedDateTime.
- Pass a
DateTimeFormatterreference to theparsemethod of the class you're instantiating.
String dateStr1 = "Feb 6, 2018";
DateTimeFormatter dateFmt = DateTimeFormatter.ofPattern("MMM d, yyyy");
LocalDate date1 = LocalDate.parse(dateStr1, dateFmt);
System.out.println(date1);
// "2018-02-06"
String dtStr1 = "February 06, 2018; 8:20 AM";
DateTimeFormatter dateTimeFmt = DateTimeFormatter.ofPattern("MMMM dd, yyyy; H:mm a");
LocalDateTime dt1 = LocalDateTime.parse(dtStr1, dateTimeFmt);
System.out.println(dt1);
// "2018-02-06T08:20"
Practice Exercise¶
The
DateTimeFormatterJavaDoc, https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html, shows pattern specifiers and examples. Some pattern letters behave differently depending on how many you use. For example,d(day of month) by itself matches9or29, butddwould match09or29, but not9.Similarly, with
Mfor month: *Mwould match or format5or12. *MMwould match or format05or12. *MMMwould match or formatMayorDec. *MMMMwould match or formatMayorDecember. *MMMMMwould match or formatMorD. *MMMMMMwould cause anIllegalArgumentException.
The input string needs to match the pattern, and the pattern itself must be self-consistent.
DateTimeFormatter dateFmt = DateTimeFormatter.ofPattern("MMM d, yyyy");
DateTimeFormatter dateTimeFmt = DateTimeFormatter.ofPattern("MMMM dd, yyyy; H:mm a");
//...
String dateStr2 = "02 6, 2018";
LocalDate date2 = LocalDate.parse(dateStr2, dateFmt);
// Exception in thread "main" java.time.format.DateTimeParseException: Text '02 6, 2018' could not be parsed at index 0
String dtStr2 = "February 06, 2018; 8:20 PM";
LocalDateTime dt2 = LocalDateTime.parse(dtStr2, dateTimeFmt);
// Exception in thread "main" java.time.format.DateTimeParseException: Text 'February 06, 2018; 8:20 PM' could not be parsed: Conflict found: Field AmPmOfDay 0 differs from AmPmOfDay 1 derived from 08:20
- Like
Integer.parseInt,DateTimeFormatterthrows unchecked exceptions you can handle with atry/catch.