0

I can't figure why when I change the format of a date then I parse it into a localDate to set it in my object, i keep getting this error :

java.time.format.DateTimeParseException: Text '07/02/2020' could not be parsed at index 0

Here's my code :

    LocalDate dateMission = mission.getDateMission(); //getting my date in yyyy/MM/dd
    LocalDate newDateMission = dateMission.plusDays(1); //Adding + 1 day in yyyy/MM/dd
    String test = DateTimeFormatter.ofPattern("dd-MM-yyyy", Locale.FRANCE).format(newDateMission); // changing format yyyy/MM/dd to dd/MM/yyyy
    LocalDate finnalDateMission = LocalDate.parse(test); //parsing string to LocalDate

    mission.setDateMission(finnalDateMission); // setting new date in LocalDate in format dd/MM/yyyy
ISSOU
  • 27
  • 7
  • 3
    What are you trying to do with this? `newMissionDate` is already a `LocalDate`. Formatting it to a `String` and back to a `LocalDate` has no effect. A `LocalDate` itself has no format, it is only in `String` form that it matters. – TiiJ7 May 19 '20 at 07:58
  • datemission and new missiondate are in format yyyy/MM/dd, so I find this method to change it into a dd/MM/yyyy but it was in a string so i needed to re parse it into a LocalDate @TiiJ7 – ISSOU May 19 '20 at 08:05
  • Ce que vous essayez est mauvais, est de travers. Il est impossible aussi. (What you are trying is wrong. It is impossible too,) – Ole V.V. May 19 '20 at 14:43
  • Isn’t this question really a duplicate of [How to change LocalDate format resulting into a LocalDate without resulting into a String \[duplicate\]](https://stackoverflow.com/questions/61339860/how-to-change-localdate-format-resulting-into-a-localdate-without-resulting-into)? And/or of [How to format LocalDate object to MM/dd/yyyy and have format persist](https://stackoverflow.com/questions/39689866/how-to-format-localdate-object-to-mm-dd-yyyy-and-have-format-persist)? – Ole V.V. May 19 '20 at 14:46

1 Answers1

2

What you're doing does not seem to make sense. LocalDate does not have a format, you will only worry about a format when you have to print it somewhere or when the input is a String. You do not need to format the date into a String and then parse it back if all you need to do is just increment one day.
You can simplify your code like this:

mission.setDateMission(mission.getDateMission().plusDays(1));

That said, your code is failing because you're generating a String with format "dd-MM-yyyy", however you're calling LocalDate.parse without specifying a format. That method uses DateTimeFormatter.ISO_LOCAL_DATE, which roughly means it has a format of "yyyy-MM-dd".

fpezzini
  • 774
  • 1
  • 8
  • 17
  • yes but this doesnt parse the format in what I need. – ISSOU May 19 '20 at 11:11
  • this set the object "DateMission" in Localdate : yyyy/MM/dd however i need to parse it in dd/MM/yyyy – ISSOU May 19 '20 at 11:12
  • this is why I wrote this, it's the only thing i could find that almost work.... – ISSOU May 19 '20 at 11:13
  • Why do you need to parse the date in that format? do you need to print it somewhere? Anyway, in order to parse the date in that format you need to call the parse method that takes in the date format: [javadoc](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html#parse-java.lang.CharSequence-java.time.format.DateTimeFormatter-) . code `LocalDate.parse(test, DateTimeFormatter.ofPattern("dd-MM-yyyy", Locale.FRANCE))` – fpezzini May 19 '20 at 13:11