繁体   English   中英

如何在 Java 8 (Scala) 中将日期时间字符串转换为 long (UNIX Epoch Time) 毫秒

[英]How to convert a date time string to long (UNIX Epoch Time) Milliseconds in Java 8 (Scala)

这个问题解决了秒的情况: How to convert a date time string to long (UNIX Epoch Time) in Java 8 (Scala)

但是如果我想要毫秒似乎我必须使用

def dateTimeStringToEpoch(s: String, pattern: String): Long = 
    LocalDateTime.parse(s, DateTimeFormatter.ofPattern(pattern))
      .atZone(ZoneId.ofOffset("UTC", ZoneOffset.ofHours(0)))
      .toInstant().toEpochMilli

这对于我在另一个问题中详述的 4 个问题来说很难看(我不喜欢的主要内容是魔术文字"UTC"和魔术数字0 )。

不幸的是,以下不能编译

def dateTimeStringToEpoch(s: String, pattern: String): Long = 
     LocalDateTime.parse(s, DateTimeFormatter.ofPattern(pattern))
                  .toEpochMilliSecond(ZoneOffset.UTC)

至于toEpochMilliSecond不存在

你不能使用LocalDateTime#atOffsetZoneOffset#UTC吗?

LocalDateTime.parse(s, dateTimeFormatter).atOffset(ZoneOffset.UTC).toInstant().toEpochMilli()

正如@Andreas 在评论中指出的那样, ZoneOffset是一个ZoneId ,所以你可以使用

def dateTimeStringToEpoch(s: String, pattern: String): Long = 
    LocalDateTime.parse(s, DateTimeFormatter.ofPattern(pattern))
      .atZone(ZoneOffset.UTC)
      .toInstant()
      .toEpochMilli()

在使用 UNIX Epoch 时,我建议使用java.time.Instant因为它是为 epoch 表示而设计的,并且默认情况下会考虑UTC ,因为它是标准的一部分。

import java.time.Instant

object InstantFormat extends App {

  //Instant.parse uses DateTimeFormatter.ISO_INSTANT
  println(Instant.parse("2019-03-12T15:15:13.147Z"))
  println(Instant.parse("2019-03-12T15:15:13Z"))
  println(Instant.parse("2019-03-12T15:15:13Z").toEpochMilli)
  println(Instant.parse("2019-03-12T15:15:13Z").getEpochSecond)
  println(Instant.ofEpochMilli(1552403713147L))
  println(Instant.ofEpochSecond(1552403713L))
}

产出

2019-03-12T15:15:13.147Z
2019-03-12T15:15:13Z
1552403713000
1552403713
2019-03-12T15:15:13.147Z
2019-03-12T15:15:13Z

您可以更改此答案以返回纪元毫秒,例如

static long dateTimeStringToEpoch(String s, String pattern) {
    return DateTimeFormatter.ofPattern(pattern).withZone(ZoneOffset.UTC)
        .parse(s, Instant::from).toEpochMilli();
}

或者,如果您甚至想避免构建临时Instant

static long dateTimeStringToEpoch(String s, String pattern) {
    return DateTimeFormatter.ofPattern(pattern).withZone(ZoneOffset.UTC)
        .parse(s, ta -> ta.getLong(ChronoField.INSTANT_SECONDS)*1000
                       +ta.get(ChronoField.MILLI_OF_SECOND));
}

请注意, DateTimeFormatter.ofPattern(pattern).withZone(ZoneOffset.UTC)ta -> ta.getLong(ChronoField.INSTANT_SECONDS)*1000+ta.get(ChronoField.MILLI_OF_SECOND)都是可重用的组件,例如您可以这样做

static final DateTimeFormatter MY_PATTERN
    = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm").withZone(ZoneOffset.UTC);
static final TemporalQuery<Long> EPOCH_MILLIS
    = ta -> ta.getLong(ChronoField.INSTANT_SECONDS)*1000+ta.get(ChronoField.MILLI_OF_SECOND);

long millis = MY_PATTERN.parse("2018-07-21 18:30", EPOCH_MILLIS);

问题是,您希望在应用程序中出现多少种不同的格式字符串。 通常,它不会像您必须解析的格式化日期那样经常更改。 创建从格式字符串到准备好的DateTimeFormatter的缓存映射可能是有益的。 无论如何,lambda 表达式是一个单例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM