为什么在Android中从毫秒转换为本地时间时时间不正确?

huangapple go评论31阅读模式
英文:

Why is the time incorrect when I convert from milliseconds to local time in Android?

问题

fun format(input: Long, outputFormat: String = "yyyy-MM-dd'T'HH:mm:ss", locale: Locale = Locale.US): String {
    val sdf = SimpleDateFormat(outputFormat, locale)
    sdf.timeZone = TimeZone.getTimeZone("UTC")
    return sdf.format(Date(input))
}
println(format(1596095340000))

Expected output:

2020-07-29T17:49:00
英文:
    fun format(input: Long, outputFormat: String = "yyyy-MM-dd'T'HH:mm:ss", locale: Locale = Locale.US): String {
        SimpleDateFormat(outputFormat, locale).apply {
        return format(Date(input))
    }

I have the following method above, when I enter the following command:

println(format(1596095340000))

I am expecting to get

2020-07-29T17:49:00

however I end up getting this instead

2020-07-30T07:49:00

Do I have to manually do the offset myself, and if I do how would I do that? I thought that this type of offset would be handled automatically?

I've also adapted answer from another SO post at https://stackoverflow.com/questions/31292032/converting-from-milliseconds-to-utc-time-in-java and results were different but still not what I expected:

    val sdf = SimpleDateFormat()
    sdf.timeZone = TimeZone.getTimeZone("PST")
    println(sdf.format(Date(1596095340000)))

7/30/20 12:49 AM

答案1

得分: 1

时间戳1596095340000对应于2020年07月30日07:49:00 UTC - 您可以在任何在线日期/时间转换站点上验证此信息。

默认情况下,SimpleDateFormat 使用系统时区。从您的输出看,系统时区与UTC相符。

要获取您期望的结果2020-07-29T17:49:00,您需要使用具有14小时偏移的时区:

var sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("GMT-1400"));
sdf.format(new Date(1596095340000L)); // "2020-07-29T17:49:00"

世界上没有14小时的时区偏移。要么您的时间戳错误,要么您期望的结果错误。例如,如果您日期错误,您期望的时区可能是澳大利亚/悉尼:

sdf.setTimeZone(TimeZone.getTimeZone("Australia/Sydney"));
sdf.format(new Date(1596095340000L)); // "2020-07-30T17:49:00"
英文:

The time stamp 1596095340000 corresponds to the instant 2020-07-30T07:49:00 UTC - you can verify this at any online date/time conversion site.

By default, SimpleDateFormat uses the system time zone. From your output, it looks like the system time zone coincides with UTC.

To get the result you expect 2020-07-29T17:49:00 you would need a time zone with a 14 hour offset:

var sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")
sdf.setTimeZone(TimeZone.getTimeZone("GMT-1400"))
sdf.format(new Date(1596095340000L)) // "2020-07-29T17:49:00"

No place in the world has a 14 hour time zone offset. Either your time stamp is wrong, or the result you expect is wrong. For example, if you got the date wrong, your expected time zone might be Australia/Sydney:

sdf.setTimeZone(TimeZone.getTimeZone("Australia/Sydney"))
sdf.format(new Date(1596095340000L)) // "2020-07-30T17:49:00"


</details>



huangapple
  • 本文由 发表于 2020年7月30日 09:17:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63164743.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定