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

Converting UTC time string to UTC milliseconds

我得到一个时间字符串,它是 UTC 格式 HH:mm

的 sunrise/sunset 次

示例:

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

所以问题是,有没有更简单的方法来做到这一点?

当然,您绝对不需要来回解析字符串。我假设 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();

请注意,这将是 return 东京太阳升起的确切时刻。将其打印为 ISO 邮票,即 2020-06-09T19:35Z

如果你真的想要匹配 2020-06-10T04:35Z 的 epoch-millis - 要清楚 没有意义,那就是 NOT 今天东京什么时候太阳升起来了! - 然后...

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,而是使用 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