As @Leo Dabus mentioned You should never escape/ignore the Z. It means UTC timezone . - as it will parse it using current timezone.
Also you shouldn't be using SimpleDateFormat
as it's outdated
Use ZonedDateTime
and OffsetDateTime
to parse the date in the specific zone.
val date = "2021-12-16T16:42:00.000Z" // your date
// date is already in Standard ISO format so you don't need custom formatted
val dateTime : ZonedDateTime = OffsetDateTime.parse(date).toZonedDateTime() // parsed date
// format date object to specific format if needed
val formatter = DateTimeFormatter.ofPattern("MMM dd, yyyy HH:mm")
Log.e("Date 1", dateTime.format(formatter)) // output : Dec 16, 2021 16:42
Above date in the UTC timezone, if you want you can convert it into your system default timezone using withZoneSameInstant
val defaultZoneTime: ZonedDateTime = dateTime.withZoneSameInstant(ZoneId.systemDefault())
Log.e("Date 1", defaultZoneTime.format(formatter)) // output : Dec 16, 2021 22:12
Note: ZonedDateTime
only works in android 8 and above, to use it below android 8 enable desugaring.
In your app module's build.gradle
, add coreLibraryDesugaringEnabled
compileOptions {
// Flag to enable support for the new language APIs
coreLibraryDesugaringEnabled true
// Sets Java compatibility to Java 8
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}