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!

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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 into my own time library. Other libs could surely profit from such a check, too.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.