我正在用Android开发

字符串的日子是 2020-04-23T23:59:59-04:00

并尝试使用以下函数将时间转换为 2020-04-23

fun changeDateFormat(strDate:String):String {
    return SimpleDateFormat("yyyy-MM-dd").format(SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZZ").parse(strDate))
}

但它显示 2020-04-24

我错过了什么吗?提前致谢。

最佳答案

这是您如何实现的

val dateInString = "2020-04-23T23:59:59-0400"
val ldt: LocalDateTime = LocalDateTime.parse(dateInString, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ"))
val currentZoneId: ZoneId = ZoneId.systemDefault()
val currentZonedDateTime: ZonedDateTime = ldt.atZone(currentZoneId)
val format: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
val formattedDate = format.format(currentZonedDateTime)
println(formattedDate)

10-04 23:10
查看更多