我正在尝试使用新的 Java 8 日期和时间 API 以及以下模式将 Instant 格式化为字符串:
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)
...
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 的字符串表示形式。
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()));
}
DateTimeFormatter.ISO_INSTANT.format(Instant.now())
这使您无需转换为 UTC。然而,其他一些语言的时间框架可能不支持毫秒,所以你应该这样做
DateTimeFormatter.ISO_INSTANT.format(Instant.now().truncatedTo(ChronoUnit.SECONDS))
Instant
类不包含区域信息,它仅存储UNIX纪元的时间戳(以毫秒为单位),即UTC的1070年1月1日。
因此,格式化程序无法打印日期,因为日期始终针对具体时区打印。
您应该为格式化程序设置时区,一切都会好起来的,就像这样:
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");
瞬间已经采用 UTC 格式,并且已经具有默认日期格式 yyyy-MM-dd。如果您对此感到满意并且不想弄乱时区或格式,您也可以
toString()
它:
Instant instant = Instant.now();
instant.toString()
output: 2020-02-06T18:01:55.648475Z
不需要 T 和 Z?(Z 表示该日期是 UTC。Z 代表“Zulu”又名“零时偏移”又名 UTC):
instant.toString().replaceAll("[TZ]", " ")
output: 2020-02-06 18:01:55.663763
想要毫秒而不是纳秒?(这样你就可以将其放入 SQL 查询中):
instant.truncatedTo(ChronoUnit.MILLIS).toString().replaceAll("[TZ]", " ")
output: 2020-02-06 18:01:55.664
等等
或者如果您仍然想使用从模式创建的格式化程序 你可以只使用 LocalDateTime 而不是 Instant:
LocalDateTime datetime = LocalDateTime.now();
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(datetime)
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MM dd");
String text = date.toString(formatter);
LocalDate date = LocalDate.parse(text, formatter);
我相信这可能会有所帮助,您可能需要使用某种本地日期变体而不是即时