获取时间持续时间在Scala中

huangapple go评论72阅读模式
英文:

Get time duration in scala

问题

我有一个包含日期时间的变量。现在我想获取该日期时间与当前时间的小时差异。在Scala中实现这一目标的最佳方式是什么?

val create_time = "2020-08-05 10:10:10"

假设当前时间如下:

val current_time = "2020-08-06 10:10:10"

现在我期望以小时为单位的持续时间。我可以使用Spark数据框使用to_timestamp()来实现这一目标,但我想看看如何在Scala中实现。

val df = Seq(("2020-08-01 12:01:19"), ("2020-08-05 12:01:19"), ("2020-08-06 16:44:55"), ("2020-08-06 16:50:59")).toDF("input_timestamp")

df.withColumn("input_timestamp", to_timestamp(col("input_timestamp")))
  .withColumn("current_timestamp", current_timestamp().as("current_timestamp"))
  .withColumn("DiffInSeconds", current_timestamp().cast(LongType) - col("input_timestamp").cast(LongType)) 
  .withColumn("DiffInHours", col("DiffInSeconds") / 3600D)
  .withColumn("DiffInDays", round(col("DiffInHours") / 24)).show(false)

以下是结果:

+-------------------+-----------------------+-------------+------------------+----------+
|input_timestamp    |current_timestamp      |DiffInSeconds|DiffInHours       |DiffInDays|
+-------------------+-----------------------+-------------+------------------+----------+
|2020-08-01 12:01:19|2020-08-06 20:25:14.775|462235       |128.3986111111111 |5.0       |
|2020-08-05 12:01:19|2020-08-06 20:25:14.775|116635       |32.39861111111111 |1.0       |
|2020-08-06 16:44:55|2020-08-06 20:25:14.775|13219        |3.6719444444444442|0.0       |
|2020-08-06 16:50:59|2020-08-06 20:25:14.775|12855        |3.5708333333333333|0.0       |
+-------------------+-----------------------+-------------+------------------+----------+
英文:

I have a variable that consists of date_time. Now I want to get the difference in hours for that date_time with current_time. What is the best way to achieve this in scala?

val create_time = "2020-08-05 10:10:10"

Let's assume if the current_time is as below:

val current_time = "2020-08-06 10:10:10"

Now I'm expecting the duration in hours. I'm able to achieve this using spark data frames using to_timestamp(), but want to see how can we do it in scala.

val df = Seq(("2020-08-01 12:01:19"), ("2020-08-05 12:01:19"), ("2020-08-06 16:44:55"), ("2020-08-06 16:50:59")).toDF("input_timestamp")


df.withColumn("input_timestamp", to_timestamp(col("input_timestamp")))
  .withColumn("current_timestamp", current_timestamp().as("current_timestamp"))
  .withColumn("DiffInSeconds", current_timestamp().cast(LongType) - col("input_timestamp").cast(LongType)) 
  .withColumn("DiffInHours",col("DiffInSeconds")/3600D)
  .withColumn("DiffInDays",round(col("DiffInHours")/24)).show(false)


+-------------------+-----------------------+-------------+------------------+----------+
|input_timestamp    |current_timestamp      |DiffInSeconds|DiffInHours       |DiffInDays|
+-------------------+-----------------------+-------------+------------------+----------+
|2020-08-01 12:01:19|2020-08-06 20:25:14.775|462235       |128.3986111111111 |5.0       |
|2020-08-05 12:01:19|2020-08-06 20:25:14.775|116635       |32.39861111111111 |1.0       |
|2020-08-06 16:44:55|2020-08-06 20:25:14.775|13219        |3.6719444444444442|0.0       |
|2020-08-06 16:50:59|2020-08-06 20:25:14.775|12855        |3.5708333333333333|0.0       |
+-------------------+-----------------------+-------------+------------------+----------+

答案1

得分: 1

import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoUnit.HOURS

val start = LocalDateTime.parse("2020-08-05 10:10:10", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
val stop = LocalDateTime.now  //current_time

val between = HOURS.between(start, stop)
//between: Long = 32
英文:

Scala solution.

import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoUnit.HOURS

val start = LocalDateTime.parse("2020-08-05 10:10:10"
              ,DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
val stop  = LocalDateTime.now  //current_time

val between = HOURS.between(start, stop)
//between: Long = 32

答案2

得分: 0

看起来处理时间和时区差异的标准方法是使用Java时间,如果您使用Java 8或更高版本。

相关的Stack Overflow帖子:

https://stackoverflow.com/questions/3614380/whats-the-standard-way-to-work-with-dates-and-times-in-scala-should-i-use-java

英文:

It looks like the standard way to deal with times and time differences is with Java time if you are using Java 8 onwards.

Related stack overflow post:

https://stackoverflow.com/questions/3614380/whats-the-standard-way-to-work-with-dates-and-times-in-scala-should-i-use-java

huangapple
  • 本文由 发表于 2020年8月7日 09:03:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63293790.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定