![](/img/trans.png)
[英]Converting a UTC ISO 8601 string to milliseconds gives a time of +12 hours
[英]Converting UTC time string to UTC milliseconds
我得到一個時間字符串,它是 UTC 的日出/日落時間,格式為HH:mm
例子:
09:35
目前我這樣做是為了使用java.time
庫將給定時間轉換為當前日期 UTC
val utcZoneId = ZoneId.of("UTC")
val now = Instant.now()
val dateTimeFormater:DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(utcZoneId)
val date = dateTimeFormater.format(now)
val fullDateSunrise = "${date}T${data[0].sunrise}:00"
val local = LocalDateTime.parse(fullDateSunrise, DateTimeFormatter.ISO_LOCAL_DATE_TIME)
val final = local.atZone(utcZoneId)
val utcSunrise = final.toInstant().toEpochMilli()
val fullDateSunset = "${date}T${data[0].sunset}:00"
val local2 = LocalDateTime.parse(fullDateSunset, DateTimeFormatter.ISO_LOCAL_DATE_TIME)
val final2 = local2.atZone(utcZoneId)
val utcSunset = final2.toInstant().toEpochMilli()
然后,一旦我擁有它們,我就會將 UTC 毫秒數傳回給客戶端
它可以按我的需要工作,但我可以提供幫助,但我覺得必須有一種更簡單的方法,而不是獲取格式化的 UTC 日期字符串並將其與給定時間相結合,然后將其轉換為實際的 DateTime object。
所以問題是,有沒有更簡單的方法來做到這一點?
當然,您絕對不需要來回解析字符串。 我假設輸入09:35
意味着:當地時間 09:35,太陽將升起。 請注意,您會混淆事物; UTC 是一個區域,像09:35
這樣的輸入是無區域的。 我懷疑這個郵票代表“UTC”; 這意味着東京今天日出的正確值是-5:25
,因為它將是19:25
,前一天,在今天太陽在東京升起的 UTC 時區。
一旦您停止使用 UTC 區域,它就會變得更加簡單:
DateTimeFormatter TIME_FORMAT = DateTimeFormatter.ofPattern("HH:mm");
LocalDateTime sunriseToday = LocalDateTime.now().with(LocalTime.parse("04:35", TIME_FORMAT));
ZonedDateTime inTokyo = sunriseToday.atZone(ZoneId.of("Asia/Tokyo"));
return inTokyo.toInstant().toEpochMilli();
請注意,這將返回太陽在東京升起的確切時間。 將其打印為 ISO 印章,即2020-06-09T19:35Z
。
如果您真的想要與2020-06-10T04:35Z
匹配的 epoch-millis - 說清楚是沒有意義的,那根本不是今天太陽在東京升起的時候。 - 然后...
DateTimeFormatter TIME_FORMAT = DateTimeFormatter.ofPattern("HH:mm");
LocalDateTime sunriseToday = LocalDateTime.now().with(LocalTime.parse("04:35", TIME_FORMAT));
ZonedDateTime inTokyo = sunriseToday.atZone(ZoneId.of("Asia/Tokyo"));
ZoneDateTime thisMakesNoSense = inTokyo.withZoneSameLocal(ZoneOffset.UTC);
return thisMakesNoSense.toInstant().toEpochMilli();
您不必轉換String
s,而是使用ZonedDateTime
並提供所需的區域。
使用一些像這樣的fun
:
fun convertToEpochMillis(time: String, zoneId: ZoneId): Long {
// parse the time to a LocalTime
val localTime = LocalTime.parse(time, DateTimeFormatter.ofPattern("HH:mm"))
// create a ZonedDateTime from the current date, the parsed time the given time zone
val zonedDateTime = ZonedDateTime.of(LocalDate.now(), localTime, zoneId)
// then return the representation of the instant in epoch millis
return zonedDateTime.toInstant().toEpochMilli()
}
並在fun main()
中使用它,如下所示
fun main() {
// define / receive a time
val time = "09:35"
// and a zone identifier
var zone = "UTC"
// use the method
val utcSunriseMillis = convertToEpochMillis(time, ZoneId.of(zone))
// and print a result statement
println("Sunrise time ${time} in ${zone} is ${utcSunriseMillis}")
// change the zone and do the same again with a different zone, just to see what happens...
zone = "America/Los_Angeles"
val laSunriseMillis = convertToEpochMillis(time, ZoneId.of(zone))
println("Sunrise time ${time} in ${zone} is ${laSunriseMillis}")
}
然后今天打印(=> 2020-06-10)
Sunrise time 09:35 in UTC is 1591781700000
Sunrise time 09:35 in America/Los_Angeles is 1591806900000
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.