1

I get a string from a mssql database representing a date like this

Feb 18 2015 12:00:00:000AM

No i tried to convert this into LocalDateTime Object wit h no luck by using

private static final DateTimeFormatter ZEITSTEMPEL_FORMAT = DateTimeFormatter.ofPattern("MMM dd yyyy HH:mm:ss:SSSa");    
LocalDateTime d = LocalDateTime.parse(getBENOETIGT_ZUM(),ZEITSTEMPEL_FORMAT);

Caused by: java.time.format.DateTimeParseException: Text 'Feb 18 2015 12:00:00:000AM' could not be parsed: Conflict found: Field AmPmOfDay 1 differs from AmPmOfDay 0 derived from 12:00

What am i missing? Thx in advance Inge

Inge
  • 427
  • 7
  • 20
  • Please edit the tags of the question, this has nothing to do with javafx-8. Add tags like `java-8`and `datetime`. – Iootu Feb 19 '15 at 13:28

1 Answers1

2

Your pattern is incorrect. You are using H: hour-of-day (0-23), but you need to use h: clock-hour-of-am-pm (1-12).

So the correct pattern would be:

DateTimeFormatter ZEITSTEMPEL_FORMAT = DateTimeFormatter.ofPattern("MMM dd yyyy hh:mm:ss:SSSa");

Iootu
  • 344
  • 1
  • 6
  • Thx for the fast response but by using this i get an other arrow Caused by: java.time.format.DateTimeParseException: Text 'Oct 21 2014 12:00:00:000AM' could not be parsed at index 0 – Inge Feb 19 '15 at 13:28
  • @Inge I wasn't able to reproduce the problem you state here. I have tried with both Strings and they are working. Are you sure you don't have empty spaces at the beginning of the string or something? It's kinda odd that the problem is at index 0. Try to use `trim()` and see if it helps. – Iootu Feb 19 '15 at 13:33
  • 2
    @Inge Have you set the locale to English (hopefully)? – Meno Hochschild Feb 19 '15 at 13:35
  • @Mneo Hochschild that was one i was missing, but it seems that the database returns some dates with extra space. eg. Feb 4 2015 12:00:00:000AM (see the two spaces between Feb and the 4?) So in this case the pattern does´t work. Is there a way to solve this using pattern? – Inge Feb 19 '15 at 14:11
  • @Inge Try the technique in [here](http://stackoverflow.com/questions/2932392/java-how-to-replace-2-or-more-spaces-with-single-space-in-string-and-delete-lead). – Iootu Feb 19 '15 at 14:15
  • got it but there was one more mistake in the pattern. instead of using dd for day-of-month i had to use single d because the day was just without leading zero...:) So using `DateTimeFormatter.ofPattern("MMM d yyyy hh:mm:ss:SSSa", Locale.ENGLISH);dateString.replaceAll("\\s+", " ").trim();` did the trick. Thx all – Inge Feb 19 '15 at 14:32