Languages

java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: YearOfEra

9 points
Asked by:
Explosssive
559

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
Answered by:
Explosssive
559

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
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join