Home > database >  Why I can't parse this date format yyyy-MM-dd'T'HH:mm:ss.SSSZ?
Why I can't parse this date format yyyy-MM-dd'T'HH:mm:ss.SSSZ?

Time:12-18

I'm trying to parse a string date to a date . My string date is : 2021-12-16T11:00:00.000Z.

I have the follwing code to parse it to a date object:

val stringDate = "2021-12-16T16:42:00.000Z"
val sdf = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
var consultationDate = sdf.parse(stringDate)

And I keep getting this error:

java.text.ParseException: Unparseable date: "2021-12-16T11:00:00.000Z"

CodePudding user response:

You should use Z the same way you use T for the parser to recognize the character in format

val stringDate = "2021-12-16T16:42:00.000Z"
val sdf = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
var consultationDate = sdf.parse(stringDate)
  • Related