Reputation: 92550
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)?
Upvotes: 11
Views: 22055
Reputation: 79620
Unfortunately, all of the existing answers are only partially correct. They lack the following important aspects of the modern date-time API:
LocalDateTime
to get a new instance of LocalDateTime
with the current timeQuestion posted by OP:
How can I parse a date like 2011-11-30 to a LocalDateTime (with a default time)?
There are two problems with your approach:
Since you are parsing a date string, you should have used LocalDate#parse
instead of LocalDateTime#parse
.
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.
Upvotes: 2
Reputation: 41
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.
Upvotes: 4
Reputation: 2338
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)?
LocalDate
LocalDateTime
atTime()
method to set your default timeNote: Use of DateTimeFormatter.ISO_LOCAL_DATE
is superfluous for parse()
, see API LocalDate#parse()
Upvotes: 10