Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTimeParseException: Text could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor

LocalDateTime.parse("2017-02-02 08:59:12", DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss"));

It prints error:

java.time.format.DateTimeParseException: Text '2017-02-02 08:59:12' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {MinuteOfHour=59, NanoOfSecond=0, SecondOfMinute=12, MicroOfSecond=0, MilliOfSecond=0, HourOfAmPm=8},ISO resolved to 2017-02-02 of type java.time.format.Parsed

Accoeding message looks like all values parsed correct, but anyway I see error.

How to make it working?

like image 761
gstackoverflow Avatar asked May 02 '17 07:05

gstackoverflow


People also ask

How do I parse LocalDateTime?

parse(CharSequence text) parse() method of a LocalDateTime class used to get an instance of LocalDateTime from a string such as '2018-10-23T17:19:33' passed as parameter. The string must have a valid date-time and is parsed using DateTimeFormatter. ISO_LOCAL_DATE_TIME.

What is the difference between LocalDate and LocalDateTime?

LocalDate – represents a date (year, month, day) LocalDateTime – same as LocalDate, but includes time with nanosecond precision. OffsetDateTime – same as LocalDateTime, but with time zone offset.

What is temporal accessor?

public interface TemporalAccessor. Framework-level interface defining read-only access to a temporal object, such as a date, time, offset or some combination of these. This is the base interface type for date, time and offset objects. It is implemented by those classes that can provide information as fields or queries.

What is zoned date-time?

ZonedDateTime is an immutable representation of a date-time with a time-zone. This class stores all date and time fields, to a precision of nanoseconds, and a time-zone, with a zone offset used to handle ambiguous local date-times.


1 Answers

I can only reproduce the exception you get when I try to parse to a LocalDateTime, so I assume that's what you want.

Your mistake is using hh (clock-hour-of-am-pm) instead of HH (hour-of-day). This works:

LocalDateTime ldt = LocalDateTime.parse("2017-02-02 08:59:12", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println(ldt);

And prints:

2017-02-02T08:59:12
like image 70
Robin Topper Avatar answered Oct 05 '22 00:10

Robin Topper