I was playing with the new date time API but when running this:
public class Test {
public static void main(String[] args){
String dateFormatted = LocalDate.now()
.format(DateTimeFormatter
.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println(dateFormatted);
}
}
It throws:
Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: HourOfDay
at java.time.LocalDate.get0(LocalDate.java:680)
at java.time.LocalDate.getLong(LocalDate.java:659)
at java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:298)
at java.time.format.DateTimeFormatterBuilder$NumberPrinterParser.format(DateTimeFormatterBuilder.java:2543)
at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2182)
at java.time.format.DateTimeFormatter.formatTo(DateTimeFormatter.java:1745)
at java.time.format.DateTimeFormatter.format(DateTimeFormatter.java:1719)
at java.time.LocalDate.format(LocalDate.java:1685)
at Test.main(Test.java:23)
When looking at the source code of the LocalDate class, I see:
private int get0(TemporalField field) {
switch ((ChronoField) field) {
case DAY_OF_WEEK: return getDayOfWeek().getValue();
case ALIGNED_DAY_OF_WEEK_IN_MONTH: return ((day - 1) % 7) + 1;
case ALIGNED_DAY_OF_WEEK_IN_YEAR: return ((getDayOfYear() - 1) % 7) + 1;
case DAY_OF_MONTH: return day;
case DAY_OF_YEAR: return getDayOfYear();
case EPOCH_DAY: throw new UnsupportedTemporalTypeException("Invalid field 'EpochDay' for get() method, use getLong() instead");
case ALIGNED_WEEK_OF_MONTH: return ((day - 1) / 7) + 1;
case ALIGNED_WEEK_OF_YEAR: return ((getDayOfYear() - 1) / 7) + 1;
case MONTH_OF_YEAR: return month;
case PROLEPTIC_MONTH: throw new UnsupportedTemporalTypeException("Invalid field 'ProlepticMonth' for get() method, use getLong() instead");
case YEAR_OF_ERA: return (year >= 1 ? year : 1 - year);
case YEAR: return year;
case ERA: return (year >= 1 ? 1 : 0);
}
throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
}
As it described in the doc:
This method will create a formatter based on a simple pattern of letters and symbols as described in the class documentation.
And all these letters are defined.
So why DateTimeFormatter.ofPattern
doesn't allow us to use some pattern letters?
I would like to add following details to the correct answer of @James_D:
Background: Most date-and-time-libraries (
java.util.Calendar
in Java, see also .Net-DateTime orDate
in JavaScript orDateTime
in Perl) are based on the concept of a universal all-purpose unique temporal type (in German there is the poetic expression "eierlegende Wollmilchsau"). In this design there cannot be an unsupported field. But the price is high: Many time problems cannot be adequately handled with such an unflexible approach because it is hard to impossible to find a common denominator for all kinds of temporal objects.JSR-310 has choosen another way, namely to allow different temporal types which consist of type-specific sets of supported built-in fields. The natural consequence is that not every possible field is supported by every type (and users can even define their own specialized fields). It is also possible to programmatically ask every object of type
TemporalAccessor
for its specific set of supported fields. ForLocalDate
we find:There is no HOUR_OF_DAY-field which explains the problem of
UnsupportedTemporalTypeException
. And if we look at the JSR-310-mapping of pattern symbols to fields we see that the symbol H is mapped to unsupported HOUR_OF_DAY:This field mapping does not mean that the field is supported by the concrete type. Parsing happens in several steps. The field mapping is only the first step. The second step is then parsing to a raw object of type
TemporalAccessor
. And finally parsing delegates to the target type (here:LocalDate
) and let it decide if it accepts all the field values in parsed intermediate object.LocalDate
represents just a date, not a DateTime. So "HH:mm:ss" make no sense when formatting aLocalDate
. Use aLocalDateTime
instead, assuming you want to represent both a date and time.The right class for me was
ZonedDateTime
which includes both Time and Time Zone.LocalDate
doesn't have the Time information so you get aUnsupportedTemporalTypeException: Unsupported field: HourOfDay
.You can use
LocalDateTime
but then you don't have the Time Zone information so if you try to access that (even by using one of the predefined formatters) you will get aUnsupportedTemporalTypeException: Unsupported field: OffsetSeconds
.