Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse an ISO date with LocalDateTime.parse(...)

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)?

like image 368
deamon Avatar asked Jan 09 '16 23:01

deamon


People also ask

How do I parse LocalDateTime?

Parsing date and time To create a LocalDateTime object from a string you can use the static LocalDateTime. parse() method. It takes a string and a DateTimeFormatter as parameter. The DateTimeFormatter is used to specify the date/time pattern.

What is ISO date format in Java?

The most common ISO Date Time Format yyyy-MM-dd'T'HH:mm:ss. SSSXXX — for example, "2000-10-31T01:30:00.000-05:00".

How do I cast a string to LocalDateTime?

String str = "2016-03-04 11:30"; DateTimeFormatter formatter = DateTimeFormatter. ofPattern("yyyy-MM-dd HH:mm"); LocalDateTime dateTime = LocalDateTime. parse(str, formatter); Done, that's all is required to convert a formatted String to transform into a LocalDateTime.

What is Z in LocalDateTime?

E.g: 2011-08-12T20:17:46.384Z. The T doesn't really stand for anything. It is just the separator that the ISO 8601 combined date-time format requires. You can read it as an abbreviation for Time. The Z stands for the Zero timezone, as it is offset by 0 from the Coordinated Universal Time (UTC).


2 Answers

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()

like image 118
Ivo Mori Avatar answered Sep 22 '22 08:09

Ivo Mori


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.

like image 41
Benjamin MARRON Avatar answered Sep 22 '22 08:09

Benjamin MARRON