正如@Leo Dabus 提到的那样你永远不应该逃避/忽略 Z。这意味着 UTC 时区。 - 因为它将使用当前时区解析它。
另外你不应该使用SimpleDateFormat,因为它是outdated
使用ZonedDateTime和OffsetDateTime解析特定区域的日期。
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
高于 UTC 时区的日期,如果需要,可以使用 withZoneSameInstant 将其转换为系统默认时区
val defaultZoneTime: ZonedDateTime = dateTime.withZoneSameInstant(ZoneId.systemDefault())
Log.e("Date 1", defaultZoneTime.format(formatter)) // output : Dec 16, 2021 22:12
注意: ZonedDateTime 仅适用于 android 8 及以上版本,在 android 8 下使用需启用脱糖功能。
在您的应用模块的build.gradle 中,添加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
}