简体   繁体   English

如何在Java 8(Scala)中将日期时间字符串转换为long(UNIX纪元时间)

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

I want the UNIX Epoch Time (Posix Time, Unix Time) of a string in some pattern , the string is in normal format (so UTC). 我想要某种pattern的字符串的UNIX纪元时间(Posix Time,Unix Time),字符串是正常格式(所以UTC)。 Please using Java 8, not Joda or old Java. 请使用Java 8,而不是Joda或旧Java。

(For milliseconds please see How to convert a date time string to long (UNIX Epoch Time) Milliseconds in Java 8 (Scala) ) (有关毫秒的信息,请参阅如何将日期时间字符串转换为long(UNIX纪元时间)Java 8中的毫秒数(Scala)

So far I have the below, but I hate this for a number of reasons: 到目前为止,我有以下内容,但我讨厌这个原因有很多:

  1. It's way too verbose for what is the most common thing to do with dates (convert to UNIX Epoch Time). 对于与日期最常见的事情(转换为UNIX纪元时间)来说,这太冗长了。 7 method calls for what should be 1. 7方法调用应该是1。
  2. It has to specify UTC, but surely UTC is just a default, why do I have to be explicit here? 它必须指定UTC,但是UTC只是默认值,为什么我必须在这里明确?
  3. It has a string literal "UTC" 它有一个字符串文字"UTC"
  4. It has a magic number ZoneOffset.ofHours(0) 它有一个神奇的数字ZoneOffset.ofHours(0)

My best so far: 我到目前为止最好的:

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

Also, bonus question, is it efficient? 另外,奖金问题,是否有效? Is there any overhead to creating the DateTimeFormatter via DateTimeFormatter.ofPattern(pattern) ? 通过DateTimeFormatter.ofPattern(pattern)创建DateTimeFormatter是否有任何开销? If so why? 如果是这样的话?

You may use the equivalent of the following Java code: 您可以使用以下Java代码的等效代码:

static long dateTimeStringToEpoch(String s, String pattern) {
    return DateTimeFormatter.ofPattern(pattern).withZone(ZoneOffset.UTC)
        .parse(s, p -> p.getLong(ChronoField.INSTANT_SECONDS));
}

Of course, processing DateTimeFormatter.ofPattern(pattern).withZone(ZoneOffset.UTC) implies work that could be avoided when encountering the same pattern string multiple times. 当然,处理DateTimeFormatter.ofPattern(pattern).withZone(ZoneOffset.UTC)意味着在多次遇到相同的模式字符串时可以避免的工作。 Whether this amount of work is relevant for your application, depends on what is is doing beside this operation. 这项工作量是否与您的应用程序相关,取决于此操作旁边的操作。

This one is more than two times shorter ( only 3 method calls ): 这个缩短了两倍以上( 只有3个方法调用 ):

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

Btw, I would build the DateTimeFormatter outside of dateTimeStringToEpoch and pass it as a method parameter: 顺便说一句,我会在dateTimeStringToEpoch之外构建DateTimeFormatter并将其作为方法参数传递:

def dateTimeStringToEpoch(s: String, formatter: DateTimeFormatter): Long = 
     LocalDateTime.parse(s, formatter).toEpochSecond(ZoneOffset.UTC)

Having actually run a performance test, there is little difference in performance (barely a factor of 2) in initialising the DateTimeFormatter outside the method. 实际运行性能测试后,初始化方法外的DateTimeFormatter性能差异很小(仅为2倍)。

scala> val pattern = "yyyy/MM/dd HH:mm:ss"
pattern: String = yyyy/MM/dd HH:mm:ss

scala>   time(() => randomDates.map(dateTimeStringToEpoch(_, pattern)))
Took: 1216

scala>   time(() => randomDates.map(dateTimeStringToEpochFixed))
Took: 732

你可以试试这个吗,根据你所说的,解析UTC时间,所以我把它作为样本。

Instant.parse("2019-01-24T12:48:14.530Z").getEpochSecond

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

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