2016-10-21 21 views
-1

Im Prinzip habe ich ein FeldJava String Local Parsen ohne Zeit Bereitstellung

private LocalDateTime date; 

, die aus String analysiert wird. Es funktioniert gut, wenn String am Ende eine Zeit angehängt hat, wird aber eine Ausnahme ausgelöst, wenn dies nicht der Fall ist.

DateTimeFormatter formatter = null; 
if (value.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}")) { 
    formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); 
} else if (value.matches("\\d{4}-\\d{2}-\\d{2}")) { 
    formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); 
} 
// Throws an exception for pattern "yyyy-MM-dd" 
date = LocalDateTime.parse(value, formatter); 

Und die Ausnahme selbst:

java.time.format.DateTimeParseException: Text '2000-06-29' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 2000-06-29 of type java.time.format.Parsed 
at pl.empirica.swissborg.service.util.CsvBeanUtils.copyList(CsvBeanUtils.java:75) ~[classes/:na] 
at pl.empirica.swissborg.service.csv.CsvReaderService.persistCsvData(CsvReaderService.java:101) ~[classes/:na] 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_91] 
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_91] 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_91] 
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_91] 
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:354) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] 
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:305) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] 
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:133) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] 
... 18 common frames omitted 
Caused by: java.lang.RuntimeException: java.time.format.DateTimeParseException: Text '2000-06-29' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 2000-06-29 of type java.time.format.Parsed 
at pl.empirica.swissborg.service.util.CsvBeanUtils.copyFields(CsvBeanUtils.java:58) ~[classes/:na] 
at pl.empirica.swissborg.service.util.CsvBeanUtils.copyList(CsvBeanUtils.java:70) ~[classes/:na] 
... 26 common frames omitted 
Caused by: java.time.format.DateTimeParseException: Text '2000-06-29' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 2000-06-29 of type java.time.format.Parsed 
at java.time.format.DateTimeFormatter.createError(Unknown Source) ~[na:1.8.0_91] 
at java.time.format.DateTimeFormatter.parse(Unknown Source) ~[na:1.8.0_91] 
at java.time.LocalDateTime.parse(Unknown Source) ~[na:1.8.0_91] 
at pl.empirica.swissborg.service.util.CsvBeanUtils.copyFields(CsvBeanUtils.java:47) ~[classes/:na] 
... 27 common frames omitted 
Caused by: java.time.DateTimeException: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 2000-06-29 of type java.time.format.Parsed 
at java.time.LocalDateTime.from(Unknown Source) ~[na:1.8.0_91] 
at java.time.format.Parsed.query(Unknown Source) ~[na:1.8.0_91] 
... 30 common frames omitted 
Caused by: java.time.DateTimeException: Unable to obtain LocalTime from TemporalAccessor: {},ISO resolved to 2000-06-29 of type java.time.format.Parsed 
at java.time.LocalTime.from(Unknown Source) ~[na:1.8.0_91] 
... 32 common frames omitted 

EDIT: zur Klärung, ich bin 100% sicher, dass Formatierer durch die zweite bedingte Verzweigung gesetzt.

+0

Überprüfen Sie, ob 'formatter' nicht null ist. Es ist vielleicht nicht durch Ihre if-Anweisung –

+0

gegangen, Sie sollten versuchen, das zu LocalDate zu analysieren. LocalDate date = LocalDate.parse (Wert, Formatierer) für Nur-Datum-Muster. Wenn Sie wirklich LocalDateTime möchten, sollten Sie das lokale Datum analysieren und die Uhrzeit mit atStartOfDay() festlegen. – Veeram

+0

Wie Veeram sagt, erwartet LocalDateTime, dass eine Zeit eingestellt wird. Es wird versuchen, es zu analysieren, kann es aber nicht finden. Verwenden Sie LocalDate oder überprüfen Sie die Antwort von @assylias –

Antwort

6

Ihr Problem ist, dass ein LocalDateTime eine Zeit braucht!

Sie haben zwei Möglichkeiten:

  • Verwendung zwei Formatierer wie Sie tun, aber der zweite Zweig sollte LocalDateTime d = LocalDate.parse(value, formatter).atStartOfDay() (zum Beispiel)
  • einen Formatierer erstellen, die beide Formate verarbeiten kann

Die zweite Option könnte folgendermaßen aussehen:

DateTimeFormatter fmt = new DateTimeFormatterBuilder() 
     .appendPattern("yyyy-MM-dd") 
     .optionalStart() 
     .appendPattern(" HH:mm") 
     .optionalEnd() 
     .parseDefaulting(ChronoField.HOUR_OF_DAY, 0) 
     .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0) 
     .toFormatter(); 

, die 2016-10-01 und 2016-10-01 10:15 analysieren können.

+0

Also ich denke, es gibt einfach keine Möglichkeit, nicht die Zeit einzustellen. Das ist schade, danke trotzdem. – Arqan

+1

Das zweite ist genial. Grundsätzlich macht genau welche Frage Anfragen mit Standard-Tools. Ich kann dieses Ding mit Java nicht überwinden, das respektable Datum/Zeit-Maschinerie hat. – SusanW

+0

http://stackoverflow.com/questions/27454025/unable-to-obtain-localdatetime-from-temporalaccessor-when-parsing-localdatetime betrachten Sie dieses mögliche Duplikat. – Zia