How to parse an ISO date with LocalDateTime.parse(...)
Asked Answered
D

3

11

I want to parse a date string like 2011-11-30 like this:

LocalDateTime.parse("2011-11-30", DateTimeFormatter.ISO_LOCAL_DATE)

But I get the following exception:

java.time.format.DateTimeParseException: Text '2011-11-30' could not be parsed:
Unable to obtain LocalDateTime from TemporalAccessor

If I pass a date and time string everything works as expected:

LocalDateTime.parse("2011-11-30T23:59:59", DateTimeFormatter.ISO_LOCAL_DATE_TIME)

How can I parse a date like 2011-11-30 to a LocalDateTime (with a default time)?

Derwin answered 9/1, 2016 at 23:2 Comment(1)
Well, use LocalDate.parse(), and then change it to a LocalDateTime by specifying which time you want.Cotopaxi
H
10

As @JB Nizet suggested, the following works

LocalDate localDate = LocalDate.parse("2011-11-30", DateTimeFormatter.ISO_LOCAL_DATE);
LocalDateTime localDateTime = localDate.atTime(23, 59, 59);
System.out.println(localDateTime); // 2011-11-30T23:59:59

How can I parse a date like 2011-11-30 to a LocalDateTime (with a default time)?

  1. Parse it first in a LocalDate
  2. Use LocalDateTime atTime() method to set your default time

Note: Use of DateTimeFormatter.ISO_LOCAL_DATE is superfluous for parse(), see API LocalDate#parse()

Homoeroticism answered 9/1, 2016 at 23:18 Comment(0)
O
4

The sample below use some magic numbers, wich should be avoid (What is a magic number, and why is it bad?).

Instead of using the method atTime(hour, minute, second),

    LocalDate localDate = LocalDate.parse("2011-11-30", DateTimeFormatter.ISO_LOCAL_DATE);
    LocalDateTime localDateTime = localDate.atTime(23, 59, 59);

you can use the LocalTime constants, such as LocalTime.MAX, (23:59:59), LocalTime.MIN (00:00:00), LocalTime.MIDNIGHT (23:59:59) or LocalTime.NOON (12:00:00)

    LocalDate localDate = LocalDate.parse("2011-11-30", DateTimeFormatter.ISO_LOCAL_DATE);
    LocalDateTime localDateTime = LocalDateTime.of(localDate, LocalTime.MIN);

It is better for maintainability and cross-reference.

Openandshut answered 24/4, 2018 at 15:50 Comment(1)
Thank you for this code snippet, which might provide some limited short-term help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you've made.Entertainment
E
2

Why I've written this answer despite existing answers

Unfortunately, all of the existing answers are only partially correct. They lack the following important aspects of the modern date-time API:

  1. Using ISO format while parsing a date string
  2. How to apply the current time to an instance of LocalDateTime to get a new instance of LocalDateTime with the current time

Question posted by OP:

How can I parse a date like 2011-11-30 to a LocalDateTime (with a default time)?

Answer

There are two problems with your approach:

  1. Since you are parsing a date string, you should have used LocalDate#parse instead of LocalDateTime#parse.

  2. Since your date string is already in the ISO pattern, you need not specify this pattern using DateTimeFormatter explicitly as LocalDate.parse uses this pattern by default.

    Apart from these two problems, you will need to use LocalDateTime#with to apply the current time to an instance of LocalDateTime to get a new instance of LocalDateTime with the current time.

    import java.time.LocalDate;
    import java.time.LocalDateTime;
    import java.time.LocalTime;
    
    public class Main {
        public static void main(String[] args) {
            // No need to specify DateTimeFormatter as the string is already in ISO format
            LocalDate date = LocalDate.parse("2011-11-30");
            System.out.println(date);
    
            // Get LocalDateTime from LocalDate
            LocalDateTime ldt = date
                                .atStartOfDay()         //Convert to LocalDateTime with 00:00
                                .with(LocalTime.now()); //Adjust to current time
    
            System.out.println(ldt);
        }
    }
    

    Output:

    2011-11-30
    2011-11-30T11:53:39.103150
    

Note: Learn more about the modern date-time API from Trail: Date Time.

Extradite answered 19/8, 2020 at 10:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.