问题
我正在尝试使用新的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)
...
#1 热门回答(164 赞)
时区
要格式化555575828atime-zone是必需的。如果没有时区,格式化程序将不知道如何将即时字段转换为人类日期时间字段,因此会抛出异常。
可以使用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
#2 热门回答(9 赞)
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");
#3 热门回答(8 赞)
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()));
}