我在字符串中获取时间和偏移量。
时间采用 UTC 格式,我必须根据偏移量转换该时间,然后将其分配给一个Calendar对象。
问题是我正在使用类plusHours()方法OffsetDateTime。我得到相同的结果。
OffsetDateTime odtB = OffsetDateTime.parse( "2018-03-26T06:00:00Z" ) ;
odtB.plusHours(2);
System.out.println(odtB);
Run Code Online (Sandbox Code Playgroud)
例如,如果我的日期是"2018-03-26T06:00:00Z"并且偏移/时区值为“+02:00”,如何更改它以获取输出"2018-03-26T08:00:00Z"?
作为输入,我得到一个类似 HH:mm 的字符串,它是 UTC。但我需要将时间转换为+3小时(即UTC+3)。
例如,原来是 12:30 - 现在变成了 15:30。
我尝试了这段代码,但它不起作用:(
fun String.formatDateTime(): String {
val sourceFormat = SimpleDateFormat("HH:mm", Locale.getDefault())
sourceFormat.timeZone = TimeZone.getTimeZone("UTC")
val parsed = sourceFormat.parse(this)
val tz = TimeZone.getTimeZone("UTC+3")
val destFormat = SimpleDateFormat("HH:mm", Locale.getDefault())
destFormat.timeZone = tz
return parsed?.let { destFormat.format(it) }.toString()
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
android simpledateformat timezone-offset android-calendar kotlin
下面的代码片段
Instant.parse("2023-08-08T00:00:00+02:00")
Run Code Online (Sandbox Code Playgroud)
按照 java-17 中的预期进行编译和执行。但是用java-8执行时,抛出以下异常
java.time.format.DateTimeParseException: Text '2023-08-01T00:00:00+02:00' could not be parsed at index 19
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.Instant.parse(Instant.java:395)
...
Run Code Online (Sandbox Code Playgroud)
我的问题是为什么?java.time api 有什么变化吗?
请注意,我确实知道解决此问题的方法,以下代码适用于 java-8
OffsetDateTime.parse("2023-08-01T00:00:00+02:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME).toInstant()
Run Code Online (Sandbox Code Playgroud)
它产生了期望的结果。我有兴趣知道在 java-time api 实现中,行为是否已经改变?
我通过XML文件提供了以下日期
2013-12-13T19:00:00+01:00
Run Code Online (Sandbox Code Playgroud)
我希望将它作为DateTime存储在我的数据库中,并使用ZERO UTC偏移量.
不幸的是,我的网络服务器有以下TimeZone信息,它会破坏我的转换逻辑......
-06:00:00
(UTC-06:00) Central Time (US & Canada)
Central Standard Time
Run Code Online (Sandbox Code Playgroud)
我怎样才能完成这个?我完全迷失了!
我需要获取某个位置的 UTC 偏移量。我因不同值的结果不一致而遇到麻烦。我需要获取的只是格式中的值+HHMM(例如,+0100“欧洲/罗马”)。
func main() {
loc, _:= time.LoadLocation("Asia/Kathmandu")
offset, others:= time.Now().In(loc).Zone()
fmt.Println(offset, others)
}
Run Code Online (Sandbox Code Playgroud)
我得到什么:
+0545合适)+07应该是+0700)MST应该是-0700)CET应该是+0100)我现在在 +3 时区。
现在 UTC 时间是 16:30 而在我这里是 19:30
我发现了一个时区问题 America/Los_Angeles
理论上是 -8 (rawOffset) 我得到了意想不到的时间。我预计如果在我的地方 20:00,那么在这个 timezoe 时间应该少 11 小时(8+3),但在现实生活中,由于夏令时,它比我的地方少 10 小时。
那么如果America/Los_Angeles我的代码中有zoneId ,我怎么能得到 -7 偏移量呢?
TimeZone.getTimeZone("America/Los_Angeles").getRawOffset()/(3600*1000)
Run Code Online (Sandbox Code Playgroud)
返回 -8 但现在它与 UTC 不同 7 小时