将UTC时间字符串转换为UTC毫秒。

问题描述 投票:0回答:1

我得到的时间字符串是UTC中的日出日落时间,格式是这样的。HH:mm

的例子。

09:35

目前,我正在做这个工作,将给定的时间转换为当前的日期UTC,使用的是 java.time 图书馆

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对象更简单的方法。

所以问题是,有没有更简单的方法?

java kotlin java-time
1个回答
2
投票

当然,你肯定不需要来回解析成字符串。我假设输入的是 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 - 说白了 没道理 今天东京的太阳根本就没有升起!- 那么...

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();

1
投票

你不需要转换 String的,使用 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
© www.soinside.com 2019 - 2024. All rights reserved.