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

时间:2020-06-10 13:06:15

标签: java kotlin java-time

我得到的时间字符串是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对象更简单的方法。

问题是,有没有更简便的方法?

2 个答案:

答案 0 :(得分:2)

当然,您绝对不需要来回解析字符串。我假设输入09:35的意思是:当地时间09:35,太阳将升起。请注意,您正在使事情变得混乱。 UTC是一个区域,像09:35这样的输入是无区域的。我怀疑这张邮票代表“ UTC”;这将意味着今天东京的日出的正确值为-5:25,因为今天的东京太阳升起的UTC时区是前一天的19:25

一旦停止使用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相匹配的纪元-明确毫无道理,那就是东京太阳升起时 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();

答案 1 :(得分: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