This question already has answers here:
Format Instant to String
(7个答案)
4年前关闭。
我正在尝试将Instant格式化为ldap日期ISO8601,但它在f.format(Instant.now())处失败; :
错误是:
(7个答案)
4年前关闭。
我正在尝试将Instant格式化为ldap日期ISO8601,但它在f.format(Instant.now())处失败; :
String input = "20161012235959.0Z";
DateTimeFormatter f = DateTimeFormatter.ofPattern ( "uuuuMMddHHmmss[,S][.S]X" );
OffsetDateTime odt = OffsetDateTime.parse ( input , f );
Instant instant = odt.toInstant ();
f.format(Instant.now());
错误是:
java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: Year
at java.time.Instant.getLong(Instant.java:603)
at java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:298)
at java.time.format.DateTimeFormatterBuilder$NumberPrinterParser.format(DateTimeFormatterBuilder.java:2540)
at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2179)
at java.time.format.DateTimeFormatter.formatTo(DateTimeFormatter.java:1746)
at java.time.format.DateTimeFormatter.format(DateTimeFormatter.java:1720)
...
...
最佳答案
要格式化Instant,需要一个时区。
String input = "20161012235959.0Z";
DateTimeFormatter f = DateTimeFormatter
.ofPattern ( "uuuuMMddHHmmss.SX" )
.withLocale( Locale.FRANCE )
.withZone( ZoneId.of("UTC"));
OffsetDateTime odt = OffsetDateTime.parse ( input , f );
Instant instant = odt.toInstant ();
System.out.println(input);
System.out.print(f.format(instant));
关于java - 不支持的字段: Year when formatting an instant to Date ISO [duplicate],我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40211892/
10-10 10:51