How to convert "2020-12-20T00:00:00.000Z" to java.util.Date?

user1688726 picture user1688726 · Jul 29, 2020 · Viewed 19.7k times · Source

I tried to use

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ENGLISH);
LocalDate date = LocalDate.parse(d.toString(), formatter);

but it throws an error.

Is there any way to convert the JSON default timestamp?

Answer

WJS picture WJS · Jul 29, 2020

You need to use LocalDateTime.

DateTimeFormatter formatter = 
        DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ENGLISH);
LocalDateTime date = LocalDateTime.parse(d.toString(), formatter);