2015-04-15 19 views
5

Bir biçimlendiricisine nasıl bu modeli kullanarak bir java.util.Date ayrıştırmak içinNasıl biçimlendirilir? Java.time?

yyyy-MM-dd HH:mm:ss Z 

için bir dize deseni var?

Sorun, bu desende zaten bulunan saat dilimi.

DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.ofInstant(dateD.toInstant(), ZoneId.systemDefault())) 

ben bir şekilde bunu ayrıştırmak Can:

String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss Z"; 

DateTimeFormatter.ofPattern(DATE_TIME_FORMAT).format(date.toInstant()); 

Bu görünüşte gibi biçim dizesi ve teklif çözümlerinde dilimini yok

java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: YearOfEra 
    at java.time.Instant.getLong(Instant.java:603) 
    at java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:298) 

burada çözümleri ve diğer yerlerde verir ?

cevap

6

bir Instant does not have the notion of year of era:

desteklenen alanlar şunlardır:

  • NANO_OF_SECOND
  • MICRO_OF_SECOND
  • MILLI_OF_SECOND
  • INSTANT_SECONDS

Onun yerine bir ZonedDateTime kullanabilirsiniz:

ZonedDateTime zdt = date.toInstant().atZone(ZoneOffset.UTC); 
String format = DateTimeFormatter.ofPattern(DATE_TIME_FORMAT).format(zdt); 
İlgili konular