1

I have a String of date time information that I'm trying to convert into a LocalDate field. The String's contents are '2019-08-28 09:00:00'. I am trying to get a MM/dd/yyyy LocalDate value to load into a JavaFX DatePicker field.

I have tried

Date date = new SimpleDateFormat("MM/dd/yyyy").parse(stringDate);

and

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
LocalDate localDate = LocalDate.parse((CharSequence) date, formatter);

both of which have returned errors. The error returned from option #2 is as follows:

Caused by: java.time.format.DateTimeParseException: Text '2019-08-30 12:00:00' could not be parsed at index 2
    at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
    at java.time.LocalDate.parse(LocalDate.java:400)
    at utils.DateTimeConverter.convertStringDateToLocalDate(DateTimeConverter.java:27)
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • In the future you should consider including the text of the error you get. It often contains useful information. – PaulProgrammer Aug 28 '19 at 21:10
  • `"2019-08-28 09:00:00"` is not very *similar* to `"MM/dd/yyyy"` - it sure can't work – user85421 Aug 28 '19 at 21:24
  • `LocalDate localDate = LocalDate.parse("2019-08-28 09:00:00", DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss"))` – Andreas Aug 28 '19 at 21:39
  • *Text '2019-08-30 12:00:00' could not be parsed at index 2* Index 2 is where `19` is in your date string. So it has parsed `20` using `MM` and now objects because there isn’t a slash where the format pattern says there should be one (don’t worry, it would later have discovered that there is no month 20). – Ole V.V. Aug 29 '19 at 03:12
  • I wonder how thoroughly you’ve searched before posting? Otherwise it’s a nice first Stack Overflow question, it has desired result, minimal example (two of them, even) and a specific problem with stack trace. Looking forward to more of those. Welcome to Stack Overflow. – Ole V.V. Aug 29 '19 at 03:17
  • 1
    Thanks for the feedback folks. I haven't used Stack Overflow to search for topics on Stack Overflow before - only Google. Having had some things pointed out, I did find that the duplicate references was a great match, and also had a good answer to this question. Thanks! – Ryan Stibich Aug 29 '19 at 14:16

2 Answers2

0

It depends of what you want to acheive ?

If the result is a Date with hours, minutes and seconds then DateFormatter is more suitable than the DateTimeFormatter

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate localDate = LocalDate.parse(date, formatter);

OR

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalTimeDate localDateWithTime = LocalTimeDate.parse(date, formatter);

The error is generated because you want to generate a date without(hours, minutes ect..) with a DateTimeFormatter using a LocalDate and not a LocalTimeDate which the correct type for your pattern format.

Note : you don't have to cast the CharSequence to String

EDIT 2 :

The pattern should be yyyy-MM-dd instead of MM-dd-yyyy if the date you want to put in the datepicker is 2019-08-28.

Here the JUnit test that valid what i'm saying :

@Test
public void testDate() {
    String date = "2019-08-30 12:00:00";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    LocalDateTime localTimeDate = LocalDateTime.parse(date, formatter);
    assertTrue(localTimeDate.toString().equals(date));
}

@Test
public void testDateWithoutTime() {
    String date = "2019-08-30";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    LocalDate localDate = LocalDate.parse(date, formatter);
    assertTrue(localDate.toString().equals(date));
}
Hassam Abdelillah
  • 2,246
  • 3
  • 16
  • 37
  • Thank you for the help on this. I am trying to return the value as a LocalDate so that it can be loaded into a DatePicker JavaFX field, formatted as MM/dd/yyyy. Unfortunately swing is out of bounds on this, so it doesn't look like I have access to DateFormatter. The relevant error is here, which occurs on the line for parsing. Caused by: java.time.format.DateTimeParseException: Text '2019-08-30 12:00:00' could not be parsed at index 2 – Ryan Stibich Aug 28 '19 at 21:32
  • Neither of those would parse input string `2019-08-28 09:00:00` – Andreas Aug 28 '19 at 21:41
  • see my edit please – Hassam Abdelillah Aug 28 '19 at 21:50
  • 1
    `MM-dd-yyyy` still doesn't parse `2019-08-28` – Andreas Aug 29 '19 at 02:03
  • I corrected the answer. Thanks – Hassam Abdelillah Aug 29 '19 at 12:51
-1

Your pattern doesn't include the relevant time information. You'll need to include that too.

Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(stringDate);
PaulProgrammer
  • 16,175
  • 4
  • 39
  • 56