-2

I faced a strange issue. I can't parse String value to LocalDate. The problem is that in runtime I paused at breakpoint and execute parsing in evaluate expression tool and it works. But then I continue application and I get an exception with next clause:

Caused by: java.time.format.DateTimeParseException: Text '-218906870-0-9088-15635' could not be parsed at index 11 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 com.library.controller.CrudController.returnBook(CrudController.java:96) ... 55 more

My code is here

LocalDate date = returnDatePicker.getValue();
    Journal item = (Journal) tableView.getSelectionModel().getSelectedItem();
    if(date != null && item != null) {
        LocalDate itemDate = LocalDate.parse(item.getStartDate().toString());
        if (date.isAfter(itemDate)) {
            journalDao.saveOrUpdate(item.withReturnDate(date));
            returnPanel.setDisable(true);
            fillJournal();
        }
    }

Date in String value looks like "2017-12-30". A few screenshots: When I evaluate expression I get correct result

How is it even possible? It works in evaluate, and doesn't in runtime. I would really appreciate if you give me a few hints.

  • 1
    LocalDate.parse() works normally, "-218906870-0-9088-15635" it's obvious that somewhere you're trying to parse that as date and that's why it's throwing exception. – whatamidoingwithmylife Dec 10 '17 at 13:52
  • @GCP it throws exception on the line LocalDate itemDate = LocalDate.parse(item.getStartDate().toString()); I debugged method parse and figured out that it throws exception during parsing. But I can't understand. Why it doesn't throw exception in evaluate expression tool, but in runtime. – Evgeny Ignatik Dec 10 '17 at 13:56
  • When we don’t know what value `item.getStartDate()` returns, we don‘t even know its type, we can only guess blindfolded. Can you please [create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve)? – Ole V.V. Dec 10 '17 at 21:11

1 Answers1

2

Casting a java.util.Date to a String and then parsing that string into a LocalDate might not be the best way to convert a Date into a LocalDate.

Try the following (explained in more detail here) and see if that fixes your problem:

Date startDate = item.getStartDate();
ZoneId zoneId = ZoneId.systemDefault(); // Or ZoneId.of( "Europe/Moscow" ) etc.
LocalDate itemDate = startDate.toInstant().atZone( zoneId ).toLocalDate();
Basil Bourque
  • 218,480
  • 72
  • 657
  • 915
Jules Dupont
  • 5,833
  • 7
  • 32
  • 35
  • 2
    Correct, calling `Date::toString` is the wrong approach. A `java.util.Date` is in UTC. It's unfortunate implementation of `toString` dynamically applies the JVM’s current default time zone. So you could be getting either of three possible dates depending on your place on the globe with a spread of at least 26 hours across time zones. So determining a date absolutely requires a time zone. Never call `Date::toString`, always convert immediately to a `java.time.Instant` as shown correctly in this Answer, `Date::toInstant`. – Basil Bourque Dec 10 '17 at 23:10
  • 1
    Given that the text that could not be parsed was `-218906870-0-9088-15635`, it hardly came from `Date.toString()`. – Ole V.V. Dec 11 '17 at 06:05
  • 2
    @OleV.V. You are correct, both I and the author of this Answer assumed a `java.util.Date` is involved. But that is not at all likely given the string value embedded in the error message in the Question. As written, the Question is unclear, lacking an explanation for that input string value. I am voting to close the Question as unclear. – Basil Bourque Dec 11 '17 at 22:14