2016-10-24 3 views
12

Я пытаюсь отформатировать Instant на дату ldap ISO8601, но он не работает в f.format (Instant.now()); :Неподдерживаемое поле: Год при форматировании момента до даты ISO

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) 
... 
... 
+0

Вы пытались использовать 'yyyy' вместо' uuuu' в шаблоне? – Rao

ответ

8

Для форматирования 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)); 
+0

Он разрешает ошибку, но я получаю что-то вроде 20161024083616,2,2 + 02 вместо чего-то вроде 20161012235959.0Z –

+0

вы отформатируете сейчас дату, поэтому ожидаемый результат: '20161024083616,2,2' –

+0

, если вы хотите получить исходный формат строки' instant' вместо 'now()'. –

Смежные вопросы