1

I am trying to parse this DateTime:

DateTimeFormatter dateTimeFormatter = new DateTimeFormatterBuilder()
                    .appendPattern("dd/MM/yyyy hh:mm:ss")
                    .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
                    .parseDefaulting(ChronoField.MILLI_OF_SECOND, 0)
                    .toFormatter();

            String ultimaAtualizacaoTexto = "17/12/2016 01:41:43";

            LocalDateTime ultimaAtualizacaoDateTime =
                    LocalDateTime.parse(ultimaAtualizacaoTexto, dateTimeFormatter);

But I am getting this error:

DateTimeParseException: Text '17/12/2016 01:41:43' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: DateTimeBuilder[fields={MilliOfSecond=0, MinuteOfHour=41, MicroOfSecond=0, NanoOfSecond=0, HourOfAmPm=1, SecondOfMinute=43}, ISO, null, 2016-12-17, null], type org.threeten.bp.format.DateTimeBuilder

I am not sure what is going on... It should totally work!

Any help with this? Thanks anyway!

Leandro Borges Ferreira
  • 12,422
  • 10
  • 53
  • 73

1 Answers1

1

So... I had to change "dd/MM/yyyy hh:mm:ss" to "dd/MM/yyyy HH:mm:ss"

Because hh is only for 12 am/pm hours. HH is for 24h.

Leandro Borges Ferreira
  • 12,422
  • 10
  • 53
  • 73
  • This is a very common error. There are tons of SO-posts containing this error. And it is not specific for Threeten or Java-8, but also common in `SimpleDateFormat` and `Joda-Time`. Some people don't even notice any problems when using "h" without am/pm-marker in formatting (effectively printing wrong data) but falsely think all is okay. This has inspired me to build some [pattern sanity checks](https://github.com/MenoData/Time4J/issues/565) into my own time library. Other libs could surely profit from such a check, too. – Meno Hochschild Dec 19 '16 at 12:18