Kotlin SimpleDateFormat解析错误的时区 [英] Kotlin SimpleDateFormat parse wrong timezone
问题描述
我的移动时区是GMT + 7,我有一个代码可以将特定的日期时间(GMT + 0)转换为特定的时区(GMT + 3):
My mobile timezone was GMT+7, I have a code to convert a specific date time(GMT+0) to a specific timezone(GMT+3):
var strDate = "2020-07-10 04:00:00+0000"
var result: Date?
var dateFormatter = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ")
dateFormatter.timeZone = TimeZone.getTimeZone("Asia/Jerusalem")
result = dateFormatter.parse(strDate)
问题是结果总是返回"星期五7月10日11:00:00 GMT + 07:00 2020 "
但是我希望它将返回日期对象"星期五7月10日07:00:00 GMT + 03:00 2020 ",知道我的代码有什么问题吗?
The problem is result always return "Fri Jul 10 11:00:00 GMT+07:00 2020"
But I expected it will return date object "Fri Jul 10 07:00:00 GMT+03:00 2020", any idea what's wrong with my code?
推荐答案
建议使用 java.time
并停止使用 java.util.Date
,java.util.Calendar
和 java.text.SimpleDateFormat
一起出现,因为这样的问题.
It's recommended to use java.time
and stop using java.util.Date
, java.util.Calendar
along with java.text.SimpleDateFormat
because of problems like this one.
在您的代码中,目标时区显然未应用到日期,但不知道为什么不适用.
In your code, the target time zone is obviously not applied to the date but it isn't obvious why it isn't.
您正在使用的模式可能是另一个问题,因为示例 String
不包含小于秒的任何时间单位,但是模式尝试考虑 .SSS
(使得代码在Kotlin游乐场失败了.
A different problem might be pattern you are using because your example String
does not contain any unit of time smaller than seconds but the pattern tries to consider .SSS
(which made the code fail in the Kotlin Playground).
切换到 java.time
并使用现代类(例如用于解析此 String
的 OffsetDateTime
)处理此问题(它不包含信息大约一个特定的时区,仅偏移零小时),然后将 ZonedDateTime
作为目标对象(这是一个实时时区,根据夏时制等因素,偏移量可能会有所不同).
Switch to java.time
and handle this with modern classes, such as OffsetDateTime
for parsing this String
(it doesn't contain information about a specific time zone, just an offset of zero hours) and ZonedDateTime
as the target object (this considers a real time zone which may have different offsets depending things like Daylight Saving Time).
您可以这样做:
import java.time.ZoneId
import java.time.ZonedDateTime
import java.time.OffsetDateTime
import java.time.format.DateTimeFormatter
fun main() {
// this example is in UTC (+0000 --> no offset / offset of 0 hours)
var strDate = "2020-07-10 04:00:00+0000"
// create a formatter that can parse Strings of this pattern
// ([] represents optional units to be parsed)
var dateFormatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss[.SSS]Z")
// and parse the String to an OffsetDateTime using this formatter
var resultOfParsing = OffsetDateTime.parse(strDate, dateFormatter)
// then print the parsed result
println(resultOfParsing)
// create the target time zone
var timeZone = ZoneId.of("Asia/Jerusalem")
// then use the target zone for a zone shift
var jerusalemTime: ZonedDateTime = resultOfParsing.atZoneSameInstant(timeZone)
// and print the result
println(jerusalemTime)
// you could use your formatter defined above for a differently formatted output, too
println(jerusalemTime.format(dateFormatter))
}
输出(包括所有中间结果)的
which outputs (including all intermediate results):
2020-07-10T04:00Z
2020-07-10T07:00+03:00[Asia/Jerusalem]
2020-07-10 07:00:00.000+0300
这篇关于Kotlin SimpleDateFormat解析错误的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!