亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將 GMT +09:00 轉換為本地時間?

如何將 GMT +09:00 轉換為本地時間?

明月笑刀無情 2023-02-16 17:14:44
當我打印從服務器獲取的日期時,它顯示Mon Jun 24 16:15:31 GMT+09:00 2019val formatter = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")val date: Date? = formatter.parse(checkedDate) // date from serverval transformedDate = ("${String.format("%02d", date!!.month + 1)}.${String.format("%02d", date!!.date)}.${date.year + 1900}")val title: String? = ("$transformedDate")val longGmtTime = date.timeval mZone = TimeZone.getDefault()val offset = mZone.getOffset(longGmtTime)val longLocalTime = longGmtTime + offset - (9 * HOUR)val localDate = Date() // local datelocalDate.time = longLocalTimeval 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結果。服務器時間和本地時間必須一致。但是當地時間顯示在其他地方。有什么問題?
查看完整描述

1 回答

?
holdtom

TA貢獻1805條經驗 獲得超10個贊

有什么問題?

嚴重問題清單包括:

  • 您正在使用設計糟糕且過時已久的 Java 日期和時間類Date,TimeZone并且SimpleDateFormat.

  • 您正在使用已棄用的方法getMonth和類。這些方法跨時區工作不可靠,這是它們被棄用的主要原因。getDategetYearDate

  • 您正在使用加法、減法和乘法手動進行時區轉換。日期和時間數學容易出錯,您應該始終將其留給經過驗證的庫方法。

  • 您獲得的毫秒計數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[亞洲/首爾]

服務器時間和客戶端時間相同,如您所要求的。相反,如果我保留自己的時區,我會得到:

clientDateTime:2019-06-24T09:15:31+02:00[歐洲/哥本哈根]

所以發生了轉換。

格式化日期:

    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 日期時間字符串。這就像2019-06-24T07:15:31Z示例中暫時使用的那樣。

問題:我可以在 Android 上將 java.time 與 minSdk API 級別 23 一起使用嗎?

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

  • 在 Java 8 及更高版本以及較新的 Android 設備(從 API 級別 26 開始)中,現代 API 是內置的。

  • 在 Java 6 和 7 中獲得 ThreeTen Backport,現代類的 backport(ThreeTen 用于 JSR 310;請參閱底部的鏈接)。

  • 在(較舊的)Android 上使用 ThreeTen Backport 的 Android 版本。它叫做 ThreeTenABP。并確保從org.threeten.bp子包中導入日期和時間類。


查看完整回答
反對 回復 2023-02-16
  • 1 回答
  • 0 關注
  • 216 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號