The Java 8 Instant is a single instantaneous point in the timeline used to record event time stamps.
You can add timezone value(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z").withZone(ZoneId.systemDefault())
) in order resolve the UnsupportedTemporalTypeException when formatting Instant to String
In this tutorial, you will learn possible ways to resolve the UnsupportedTemporalTypeException
when formatting Instant to String in Java.
Add Time Zone to DateTimeFormatter
The DateTimeFormatter in Java is used for parsing date-time objects.
Cause of Error
When parsing Instant value to String, Instant value requires time zone value . If the time zone value is not specified, then UnsupportedTemporalTypeException
will be thrown.
Code
import java.time.Instant;
import java.time.format.DateTimeFormatter;
Instant instant = Instant.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z");
String date = dateTimeFormatter.format(instant);
System.out.println(date);
Output
Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: YearOfEra
at java.base/java.time.Instant.getLong(Instant.java:605)
In the above example, the time zone value is not set when parsing an Instant value to String, resulting in an UnsupportedTemporalTypeException
.
You can add a time zone value to the DateTimeFormatter
to avoid the UnsupportedTemporalTypeException
error, as shown below,
Code
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Locale;
Instant instant = Instant.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z").withZone(ZoneId.systemDefault());
String date = dateTimeFormatter .format(instant);
System.out.println(date); //2023-01-29 20:02:39 GMT
DateTimeFormatter dateTimeFormatter1 =
DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL )
.withLocale( Locale.US )
.withZone( ZoneOffset.UTC );
String date1 = dateTimeFormatter1.format(instant);
System.out.println(date1); //Sunday, January 29, 2023 at 9:30:17 PM Z
Use Predefined ISO date-time formatter
The ISO default set of date-time format can also be used to avoid the UnsupportedTemporalTypeException
when parsing Instant value to String.
You can find different predefined ISO date-time formatter with examples in this tutorial, How to parse/format dates with Java 8 LocalDateTime
Code
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
Instant instant = Instant.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME.withZone(ZoneId.from(ZoneOffset.UTC));
String date = dateTimeFormatter.format(instant);
System.out.println(date); //2023-01-29T21:23:05.5036373
Use LocalDateTime instead of Instant
Using LocalDateTime instead of Instant, as shown below, can also help achieve the same objective without the UnsupportedTemporalTypeException
error.
Code
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
LocalDateTime datetime = LocalDateTime.now();
String date = DateTimeFormatter.ofPattern(""yyyy-MM-dd HH:mm:ss").format(datetime);
System.out.println(date); //2023-01-29 21:36:14