0

java.time.format.DateTimeParseException: Text '2020-11-09 04:59:59.203' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {},ISO resolved to 2020-11-09 04:59:59.203 of type java.time.format.Parsed

LocalDate localDate=  add90Days("2020-11-09 04:59:59.203",2);
 Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
 
 Employee e = new Employee();
 e.setEndDate(date);
 
 
 private static LocalDate add90Days(String rentalEndDate,int days) {
        ZonedDateTime odt90DaysLater=null;
        try {
            DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
            ZonedDateTime odt = ZonedDateTime.parse(rentalEndDate, inputFormatter);

             odt90DaysLater = odt.plusDays(days);
                
        } catch (Exception e) {
            e.printStackTrace();
        }

        return odt90DaysLater.toLocalDate();
    }
user739115
  • 1,117
  • 5
  • 20
  • 41

2 Answers2

1

I think the exception occurred because in your code, you are not providing a default time zone while conversion neither are you providing a time zone in the input to be parsed. So , try setting a default time zone or providing one in the input :

DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
ZonedDateTime zdt = 
  ZonedDateTime.parse(rentalEndDate, inputFormatter .withZone(ZoneId.systemDefault()));

or try provide input like :

2019-03-27T10:15:30+05:30

and use appropriate parser for that. If you do not necessarily need the timezone data then use LocalDateTime .

Ananthapadmanabhan
  • 5,706
  • 6
  • 22
  • 39
  • --error is coming as could not be parsed, unparsed text found at index 21 DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S"); ZonedDateTime zdt = ZonedDateTime.parse("2020-11-09 04:59:59.203", inputFormatter.withZone(ZoneId.systemDefault())); – user739115 Nov 17 '20 at 10:17
  • @user739115 Try using pattern as `"yyyy-MM-dd HH:mm:ss.SSS"` – Ananthapadmanabhan Nov 17 '20 at 11:38
1

You are using a wrong class

Your date-time string, 2020-11-09 04:59:59.203 does not have time-zone information and therefore, the most appropriate class for it to be parsed into is LocalDateTime.

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        LocalDate localDate = add90Days("2020-11-09 04:59:59.203", 2);
        System.out.println(localDate);
    }

    private static LocalDate add90Days(String rentalEndDate, int days) {
        DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS");
        return LocalDateTime.parse(rentalEndDate, inputFormatter).plusDays(days).toLocalDate();
    }
}

Output:

2020-11-11

I can see that you are also using java.util.Date. The date-time API of java.util and their formatting API, SimpleDateFormat are outdated and error-prone. I suggest you should stop using them completely and switch to the modern date-time API.

Learn more about the modern date-time API at Trail: Date Time.

If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110