将Instant格式化为字符串时出现UnsupportedTemporalTypeException

我试图使用新的Java 8日期和时间API和以下模式格式化一个即时字符串:

Instant instant = ...;
String out = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(instant);

使用上面的代码,我得到了一个异常,抱怨一个不支持的字段:

java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: YearOfEra
at java.time.Instant.getLong(Instant.java:608)
at java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:298)
...
330187 次浏览
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MM dd");
String text = date.toString(formatter);
LocalDate date = LocalDate.parse(text, formatter);

我相信这可能会有帮助,你可能需要使用某种本地日期变化,而不是即时

Instant类不包含Zone信息,它只存储以毫秒为单位的时间戳,即从UTC开始的1月1070日。 因此,格式化程序不能打印日期,因为日期总是为具体时区打印。 你应该将time zone设置为formatter,一切正常,就像这样:

Instant instant = Instant.ofEpochMilli(92554380000L);
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withLocale(Locale.UK).withZone(ZoneOffset.UTC);
assert formatter.format(instant).equals("07/12/72 05:33");
assert instant.toString().equals("1972-12-07T05:33:00Z");

时区

要格式化Instant,必须使用时区。如果没有时区,格式化程序就不知道如何将即时字段转换为人工日期-时间字段,因此会引发异常。

可以使用withZone()将时区直接添加到格式化程序中。

DateTimeFormatter formatter =
DateTimeFormatter.ofLocalizedDateTime( FormatStyle.SHORT )
.withLocale( Locale.UK )
.withZone( ZoneId.systemDefault() );

如果你特别想要一个没有显式时区的ISO-8601格式 (正如OP要求的那样),使用时区隐式UTC,你需要

DateTimeFormatter.ISO_LOCAL_DATE_TIME.withZone(ZoneId.from(ZoneOffset.UTC))

生成字符串

现在使用该格式化程序生成Instant的String表示形式。

Instant instant = Instant.now();
String output = formatter.format( instant );

转储到控制台。

System.out.println("formatter: " + formatter + " with zone: " + formatter.getZone() + " and Locale: " + formatter.getLocale() );
System.out.println("instant: " + instant );
System.out.println("output: " + output );

运行时。

formatter: Localized(SHORT,SHORT) with zone: US/Pacific and Locale: en_GB
instant: 2015-06-02T21:34:33.616Z
output: 02/06/15 14:34
public static void main(String[] args) {


DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
.withZone(ZoneId.systemDefault());


System.out.println(DATE_TIME_FORMATTER.format(new Date().toInstant()));


}

或者如果你仍然想使用由pattern创建的formatter 你可以使用LocalDateTime而不是Instant:

LocalDateTime datetime = LocalDateTime.now();
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(datetime)
DateTimeFormatter.ISO_INSTANT.format(Instant.now())

这使您不必转换为UTC。但是,一些其他语言的时间框架可能不支持毫秒,所以您应该这样做

DateTimeFormatter.ISO_INSTANT.format(Instant.now().truncatedTo(ChronoUnit.SECONDS))

瞬时已经是UTC,并且已经有一个默认的日期格式yyyy-MM-dd。如果你对此感到满意,并且不想打乱时区或格式,你也可以toString()它:

Instant instant = Instant.now();
instant.toString()
output: 2020-02-06T18:01:55.648475Z
< p > < br > 不想要T和Z? (Z表示此日期为UTC。Z代表“Zulu”,又名“零时偏移”,又名UTC):

instant.toString().replaceAll("[TZ]", " ")
output: 2020-02-06 18:01:55.663763
< p > < br > 想要毫秒而不是纳秒?(所以你可以把它放入sql查询中):

instant.truncatedTo(ChronoUnit.MILLIS).toString().replaceAll("[TZ]", " ")
output: 2020-02-06 18:01:55.664

等。