我正在尝试使用新的java 8 time-api和模式将Instant格式化为String:
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() );
生成字符串
现在使用该格式化程序生成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
谢谢!! BTW例外的"不支持的字段"指向例如年,是非常钝的。也许应该检测到这种情况,并且应该抛出直接指向Instant中缺少的区域id的异常!
更奇怪的是,如果你包括.withZone(例如.withZone(ZoneId.of("Z")))并格式化LocalDateTime,那么该区域就是IGNORED!因此,只要包含.withZone(),就可以对Instant和LocalDateTime使用相同的格式化程序,而不会影响后者显示的时间。
为什么接受Instant有一个GMT的TimeZone是如此困难?
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()));
}
虽然此代码可能会回答这个问题,但提供有关如何以及为何解决问题的其他背景将提高答案的长期价值。
虽然此代码段可以解决问题,但包括解释确实有助于提高帖子的质量。请记住,您将来会回答读者的问题,而这些人可能不知道您的代码建议的原因。
Instant类不包含区域信息,它仅存储UNIX纪元的时间戳(以毫秒为单位),即UTC的1月1日1070。
因此,格式化程序无法打印日期,因为日期始终打印为具体时区。
您应该将时区设置为格式化程序,一切都会好的,如下所示:
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");
DateTimeFormatter.ISO_INSTANT.format(Instant.now())
这样您就不必转换为UTC。但是,其他一些语言的时间框架可能不支持毫秒,所以你应该这样做
DateTimeFormatter.ISO_INSTANT.format(Instant.now().truncatedTo(ChronoUnit.SECONDS))
你是什??么意思"其他语言的时间框架"? ISO_INSTANT.format()会自动截断为秒吗?
有些框架只希望时间达到秒,因为它们不遵循完整的ISO约定,并且当输入字符串的一部分有毫秒时就会中断。
或者,如果您仍想使用从模式创建的格式化程序
你可以使用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);
我相信这可能会有所帮助,您可能需要使用某种localdate变体而不是即时变体