日期到当地时间 kotlin

Sar*_*her 1 java android date kotlin

我有一个日期对象

“格林威治标准时间 4 月 26 日星期日 11:44:00+03:00 2020”

我试图格式化它,但它总是会忽略时区“+3:00”并显示小时而不更改它

我试过的:

   val sdf = SimpleDateFormat("dd MM yyyy HH:mm:ss", Locale.US)
    return sdf.format(this)
Run Code Online (Sandbox Code Playgroud)

kau*_*nge 5

我不完全理解您的问题,但我猜您的意思是您需要与特定区域相关的 DateTime。在 kotlin 我们有ZonedDateTime

import java.time.ZoneId
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle

val londonZone = ZoneId.of("Europe/London")
val londonCurrentDateTime = ZonedDateTime.now(londonZone)
println(londonCurrentDateTime) // Output:  2018-01-25T07:41:02.296Z[Europe/London]
val londonDateAndTime = londonCurrentDateTime.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL, FormatStyle.MEDIUM))
println(londonDateAndTime) // Output: Thursday, January 25, 2018 7:40:34 AM
Run Code Online (Sandbox Code Playgroud)

您可能还需要查看LocalDateTime课程。