Unable to obtain LocalDateTime from TemporalAccessor when parsing LocalDateTime (Java 8)
Asked Answered
R

13

256

I am simply trying to convert a date string into a DateTime object in Java 8. Upon running the following lines:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDateTime dt = LocalDateTime.parse("20140218", formatter);

I get the following error:

Exception in thread "main" java.time.format.DateTimeParseException: 
Text '20140218' could not be parsed: 
Unable to obtain LocalDateTime from TemporalAccessor: 
{},ISO resolved to 2014-02-18 of type java.time.format.Parsed
    at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1918)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1853)
    at java.time.LocalDateTime.parse(LocalDateTime.java:492)

The syntax is identical to what has been suggested here, yet I am served with an exception. I am using JDK-8u25.

Representational answered 13/12, 2014 at 0:4 Comment(1)
why are you using LocalDateTime without a time?Cusped
R
262

It turns out Java does not accept a bare Date value as DateTime. Using LocalDate instead of LocalDateTime solves the issue:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDate dt = LocalDate.parse("20140218", formatter);
Representational answered 13/12, 2014 at 0:20 Comment(6)
For what it's worth: I had this same problem even when using LocalDate and not LocalDateTime. The issue was that I had created my DateTimeFormatter using .withResolverStyle(ResolverStyle.STRICT);, so I had to use date pattern uuuuMMdd instead of yyyyMMdd (i.e. "year" instead of "year-of-era")!Wintery
@ZeroOne's comment should be included as part of the selected answer.Bon
@Bon I've posted my comment as a separate answer, you may upvote that one. Eventually it will surpass the accepted one in the number of votes...Wintery
@iulian-david 's answer was more helpful here and actually solved the problem of parsing a datetime that may or may not have a time part.Alcestis
"Java does not accept a bare Date value as DateTime"! What a rediculous feature!Sideswipe
For those who don't want to convert to localeDate see Julian David answerStreeter
S
110

If you really need to transform a date to a LocalDateTime object, you could use the LocalDate.atStartOfDay(). This will give you a LocalDateTime object at the specified date, having the hour, minute and second fields set to 0:

final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDateTime time = LocalDate.parse("20140218", formatter).atStartOfDay();
Socialite answered 1/10, 2015 at 11:33 Comment(3)
Correction: set to whatever time it would be at the start of that day.Mats
LocalDateTime.from seems unnecessary, as atStartOfDay() already returns LocalDateTime.Lumbar
scala: val time = LocalDate.parse("20171220", DateTimeFormatter.ofPattern("yyyyMMdd")).atStartOfDay.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))Bailsman
P
91

For what is worth if anyone should read again this topic(like me) the correct answer would be in DateTimeFormatter definition, e.g.:

private static DateTimeFormatter DATE_FORMAT =  
            new DateTimeFormatterBuilder().appendPattern("dd/MM/yyyy[ [HH][:mm][:ss][.SSS]]")
            .parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
            .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
            .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
            .toFormatter(); 

One should set the optional fields if they will appear. And the rest of code should be exactly the same.

Edit : usefull thing from wittyameta comment :

Remember to add the parseDefaulting AFTER you have called appendPattern. Otherwise it'll give DateTimeParseException

Phiphenomenon answered 25/9, 2016 at 9:11 Comment(5)
This is true universal solution for instance for such method: Long getFileTimestamp(String file, Pattern pattern, DateTimeFormatter dtf, int group); Here you have different patterns and DateTimeFormetter, w/ and w/o time specified.Translate
Can it be a static field? I have not found in the Javadoc that an instance created that way is thread-safeBurgwell
Remember to add the parseDefaulting AFTER you have called appendPattern. Otherwise it'll give DateTimeParseException.Gambit
when using HH for hours in the pattern, i get 00 for AM hoursAshmead
If you want to parse fraction of second by JAVA 8, try to visit this log. #22588551Lecroy
G
48

For anyone who landed here with this error, like I did:

Unable to obtain LocalDateTime from TemporalAccessor: {HourOfAmPm=0, MinuteOfHour=0}

It came from a the following line:

LocalDateTime.parse(date, DateTimeFormatter.ofPattern("M/d/yy h:mm"));

It turned out that it was because I was using a 12hr Hour pattern on a 0 hour, instead of a 24hr pattern.

Changing the hour to 24hr pattern by using a capital H fixes it:

LocalDateTime.parse(date, DateTimeFormatter.ofPattern("M/d/yy H:mm"));
Grueling answered 17/5, 2018 at 19:18 Comment(0)
A
42

This is a really unclear and unhelpful error message. After much trial and error I found that LocalDateTime will give the above error if you do not attempt to parse a time. By using LocalDate instead, it works without erroring.

This is poorly documented and the related exception is very unhelpful.

Amianthus answered 26/1, 2015 at 23:33 Comment(0)
W
37

Expanding on retrography's answer..: I had this same problem even when using LocalDate and not LocalDateTime. The issue was that I had created my DateTimeFormatter using .withResolverStyle(ResolverStyle.STRICT);, so I had to use date pattern uuuuMMdd instead of yyyyMMdd (i.e. "year" instead of "year-of-era")!

DateTimeFormatter formatter = new DateTimeFormatterBuilder()
  .parseStrict()
  .appendPattern("uuuuMMdd")
  .toFormatter()
  .withResolverStyle(ResolverStyle.STRICT);
LocalDate dt = LocalDate.parse("20140218", formatter);

(This solution was originally a comment to retrography's answer, but I was encouraged to post it as a stand-alone answer because it apparently works really well for many people.)

Wintery answered 28/4, 2017 at 8:10 Comment(1)
The answer to why "u" instead of "y"has been answered in this link uuuu-versus-yyyy-in-datetimeformatter-formatting-pattern-codes-in-java @PeruEvzone
E
19

If the date String does not include any value for hours, minutes and etc you cannot directly convert this to a LocalDateTime. You can only convert it to a LocalDate, because the string only represent the year,month and date components it would be the correct thing to do.

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDate ld = LocalDate.parse("20180306", dtf); // 2018-03-06

Anyway you can convert this to LocalDateTime.

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDate ld = LocalDate.parse("20180306", dtf);
LocalDateTime ldt = LocalDateTime.of(ld, LocalTime.of(0,0)); // 2018-03-06T00:00
Excommunicative answered 12/3, 2018 at 9:38 Comment(2)
Also make sure the format is correct. This fix didn't work for me until because I set the format to yyyy-mm-dd when it should have been yyyy-MM-dd.Thalassa
I want to upvote this 10 times if I canCrave
B
6
 DateTimeFormatter format = new DateTimeFormatterBuilder()
                            .appendPattern("yyyy-MM-dd")
                            .parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
                            .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
                            .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
                            .parseDefaulting(ChronoField.MILLI_OF_SECOND, 0)
                            .toFormatter();

Works for me

Benco answered 11/10, 2021 at 9:37 Comment(1)
for minimum, only ChronoField.HOUR_OF_DAY is needed, it does the same thing with the other 3 removed (MINUTE_OF_HOUR, SECOND_OF_MINUTE, MILLI_OF_SECOND)Dorotea
W
4

You do not need to define a DateTimeFormatter

You do not need to define a DateTimeFormatter to parse the given date string. You can use the OOTB (Out-Of-The-Box), DateTimeFormatter.BASIC_ISO_DATE to parse it.

Demo:

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        LocalDate date = LocalDate.parse("20140218", DateTimeFormatter.BASIC_ISO_DATE);
        System.out.println(date);

        // In case you need an instance of LocalDateTime
        LocalDateTime ldt = date.atTime(LocalTime.MIN);
        System.out.println(ldt);
    }
}

Output:

2014-02-18
2014-02-18T00:00

ONLINE DEMO

Learn more about the modern Date-Time API* from Trail: Date Time.


* If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring. Note that Android 8.0 Oreo already provides support for java.time. Check this answer and this answer to learn how to use java.time API with JDBC.

Whitethorn answered 6/11, 2021 at 10:28 Comment(0)
P
3

In cases where you simply want to take a format (whether or not it has time) and want to parse to a LocalDateTime, you can do the following.

LocalDateTime parseDateTime(String dateTime, DateTimeFormatter fmt) {
  return fmt.parse(dateTime, t -> {
    LocalDate date = t.query(TemporalQueries.localDate());
    LocalTime time = t.query(TemporalQueries.localTime());
    return LocalDateTime.of(date, time != null ? time : LocalTime.MIDNIGHT);
  });
}

I needed this because I was getting the date/time pattern as a parameter for a custom Spark UDF.

Periosteum answered 4/11, 2021 at 0:43 Comment(0)
P
2

This works fine

public class DateDemo {
    public static void main(String[] args) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy hh:mm");
        String date = "16-08-2018 12:10";
        LocalDate localDate = LocalDate.parse(date, formatter);
        System.out.println("VALUE="+localDate);

        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm");
        LocalDateTime parse = LocalDateTime.parse(date, formatter1);
        System.out.println("VALUE1="+parse);
    }
}

output:

VALUE=2018-08-16
VALUE1=2018-08-16T12:10
Presentative answered 4/12, 2018 at 15:21 Comment(0)
D
0

Try this one:

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM-dd-yyyy"); 
LocalDate fromLocalDate = LocalDate.parse(fromdstrong textate, dateTimeFormatter);

You can add any format you want. That works for me!

Doti answered 7/11, 2018 at 21:9 Comment(0)
V
0

I arrived at this problem because my input string didn't have a year in it:

input string: Tuesday, June 8 at 10:00 PM
formatter: DateTimeFormatter.ofPattern("EEEE, MMMM d 'at' h:mm a", Locale.US);

I knew the year so I just appended it to get:

input string: Tuesday, June 8 at 6:30 PM 2021
formatter: DateTimeFormatter.ofPattern("EEEE, MMMM d 'at' h:mm a uuuu", Locale.US);

Venusberg answered 8/6, 2021 at 20:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.