EN
java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: YearOfEra
1
answers
9
points
Any idea, what is the reasone of the following excatiopn?:
Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: YearOfEra
at java.base/java.time.Instant.getLong(Instant.java:602)
at java.base/java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:308)
at java.base/java.time.format.DateTimeFormatterBuilder$NumberPrinterParser.format(DateTimeFormatterBuilder.java:2702)
at java.base/java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2341)
at java.base/java.time.format.DateTimeFormatter.formatTo(DateTimeFormatter.java:1843)
at java.base/java.time.format.DateTimeFormatter.format(DateTimeFormatter.java:1817)
at Program.main(Program.java:14)
Example source code:
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Program {
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd", Locale.ROOT);
Instant instant = Instant.now();
String string = formatter.format(instant);
System.out.println(string);
}
}
1 answer
1
points
It looks like, there is not set zone.
Check this:
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Program {
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd", Locale.ROOT)
.withZone(ZoneOffset.UTC);
Instant instant = Instant.now();
String string = formatter.format(instant);
System.out.println(string);
}
}
0 comments
Add comment