如何将 GMT +09:00 转换为本地时间?

How to convert GMT +09:00 to local time?

当我打印从服务器获取的日期时,它显示 Mon Jun 24 16:15:31 GMT+09:00 2019

val formatter = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
val date: Date? = formatter.parse(checkedDate) // date from server
val transformedDate = ("${String.format("%02d", date!!.month + 1)}.${String.format("%02d", date!!.date)}.${date.year + 1900}")
val title: String? = ("$transformedDate")

val longGmtTime = date.time
val mZone = TimeZone.getDefault()
val offset = mZone.getOffset(longGmtTime)
val longLocalTime = longGmtTime + offset - (9 * HOUR)

val localDate = Date() // local date
localDate.time = longLocalTime
val localFormatTime = formatter.format(localDate)
val transformedLocalDate = ("${String.format("%02d", localDate!!.month + 1)}.${String.format("%02d", localDate!!.date)}.${localDate.year + 1900}")

它给了我 server time: 2019-06-24 16:15:31 -> 06.24.2019, local time(Asia/Seoul)-> 2019-06-25 01:15:30 ->06.25.2019 结果。

服务器时间和本地时间必须一致。但是当地时间显示在其他地方。

有什么问题?

您应该指定设置服务器时区而不是您的设备(这是默认设置)

What's the problem?

总问题列表包括:

  • 您使用的是设计糟糕且过时已久的 Java 日期和时间 classes DateTimeZoneSimpleDateFormat
  • 您正在使用 Date class 中已弃用的方法 getMonthgetDategetYear。这些方法跨时区工作不可靠,这是它们被弃用的主要原因。
  • 您正在使用加法、减法和乘法手动进行时区转换。日期和时间数学是 error-prone,您应该始终将其留给经过验证的库方法。
  • 您从 Date.getTime 获得的毫秒数是从 1970-01-01T00:00:00 UTC 开始计算的。这是一个独特的时刻,与时区无关,因此在时区转换的毫秒计数中增加和减少是没有意义的。
  • 当我将 JVM 的默认时区设置为 Asia/Seoul 并假设 HOUR 为 0(或 0 到 111 范围内的某个值)时,我可以重现您的结果。我假设您希望 HOUR 表示一小时内的毫秒数,3 600 000(至少通常存在例外)。
  • 您正在通过连接 Strirg.format 的调用结果来格式化您的日期。最好将格式留给专门的日期格式化程序。

修复:java.time

    ZoneId serverTimeZone = ZoneId.of("Asia/Seoul");
    DateTimeFormatter serverFormatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
    ZoneId clientTimeZone = ZoneId.systemDefault();

    String checkedDate = "2019-06-24 16:15:31";
    ZonedDateTime serverDateTime = LocalDateTime.parse(checkedDate, serverFormatter)
            .atZone(serverTimeZone);
    ZonedDateTime clientDateTime = serverDateTime.withZoneSameInstant(clientTimeZone);
    System.out.println("clientDateTime: " + clientDateTime);

对不起,我只会运行写Java代码,我相信你会翻译。在我的 JVM 时区仍设置为 Asia/Seoul 的情况下,我得到:

clientDateTime: 2019-06-24T16:15:31+09:00[Asia/Seoul]

服务器时间和客户端时间相同,符合您的要求。相反,如果我保留自己的时区,我会得到:

clientDateTime: 2019-06-24T09:15:31+02:00[Europe/Copenhagen]

因此发生了转换。

要格式化日期:

    DateTimeFormatter displayFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)
            .withLocale(Locale.forLanguageTag("ko-KR"));
    String transformedLocalDate = clientDateTime.format(displayFormatter);
    System.out.println("transformedLocalDate: " + transformedLocalDate);

transformedLocalDate: 2019. 6. 24.

或者如果你坚持 month.date.year:

    DateTimeFormatter displayFormatter = DateTimeFormatter.ofPattern("MM.dd.u");

transformedLocalDate: 06.24.2019

进一步的建议是让您的服务器以 ISO 8601 格式提供 UTC 格式的 date-time 字符串。在示例中使用的那一刻就像 2019-06-24T07:15:31Z

问题:我可以在 Android 上将 java.time 与 minSdk API 级别 23 一起使用吗?

是的,java.time 在新旧 Android 设备上都能很好地工作。它只需要至少 Java 6.

  • 在 Java 8 和更新的 Android 设备上(从 API 级别 26)现代 API 出现 built-in.
  • 在 Java 6 和 7 中获取 ThreeTen Backport,现代 classes 的 backport(ThreeTen 用于 JSR 310;请参阅底部的链接)。
  • 在(较旧的)Android 使用 ThreeTen Backport 的 Android 版本。它叫做 ThreeTenABP。并确保使用子包从 org.threeten.bp 导入日期和时间 classes。

链接