Error: Unsupported field: HourOfDay
code show as below
LocalDate now = LocalDate.now();
String year = now.format(DateTimeFormatter.ofPattern("yyyy"));
String hour = now.format(DateTimeFormatter.ofPattern("MM-dd-HH"));
The error is as follows
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:2540)
at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2179)
at java.time.format.DateTimeFormatter.formatTo(DateTimeFormatter.java:1746)
at java.time.format.DateTimeFormatter.format(DateTimeFormatter.java:1720)
at java.time.LocalDate.format(LocalDate.java:1691)
at com.feiyangshop.recommendation.HdfsHandler.main(HdfsHandler.java:21)
I also traced it to the jdk source code to view it.
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);
}
Finally, the code that throws the exception is here.
I explained in now.format(DateTimeFormatter.ofPattern("MM-dd-HH"));
the HH
is not case
among.
Why case
is there only year, month and day, and no hour, minute and second?
emmmmm.
The answer is: I used the wrong class
LocalDateTime
This class should be used . This class contains hours, minutes and seconds.
LocalDateTime now = LocalDateTime.now();
String year = now.format(DateTimeFormatter.ofPattern("yyyy"));
String hour = now.format(DateTimeFormatter.ofPattern("MM-dd-HH"));
Read More:
- How to Solve Error: Missing type map configuration or unsupported mapping
- OpenCV(-206:Bad flag (parameter or structure field)) Unrecognized or unsupported array type [How to Solve]
- How to Solve Logstash error: failed to execute action
- TypeError: unsupported format string passed to numpy.ndarray.__format__
- How to Solve Error: Type mismatch: cannot convert from Object to Car
- [ERROR] Terminal initialization failed; falling back to unsupported java.lang.IncompatibleClassChang
- How to Solve Spring Cloud Error context has been closed already
- Hbase Shell startup error: [ERROR] Terminal initialization failed; falling back to unsupported
- How to Solve error C2039: “to_ String “: not a member of” STD “
- How to Solve Namedparameterjdbctemplate.queryforobject() Return Error
- How to Solve Springboot Error: Failed to convert value of type
- How to Solve forEach cannot exit the loop Issue
- Android 10: How to Solve Systemui Error
- How to Solve Jetpack room use error
- How to Solve OpenCV CVUI Error: LINK2019
- How to Solve ES error: “illegal_argument_exception”
- How to Solve LeetCode Error: AddressSanitizer:DEADLYSIGNAL
- How to Solve Starrocks Various Error
- [Solved] Android 5.1 code compilation error: Unsupported reloc 43
- How to Solve linq Statement IsNullOrWhiteSpace Error