将 Java 转换为 Scala 持续时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32076311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 07:28:39  来源:igfitidea点击:

Converting Java to Scala durations

scalaconfigdurationscala-java-interop

提问by Jacob Eckel

Is there an elegant way to convert java.time.Durationto scala.concurrent.duration.FiniteDuration?

有没有一种优雅的方式来转换java.time.Durationscala.concurrent.duration.FiniteDuration

I am trying to do the following simple use of Config in Scala:

我正在尝试在 Scala 中对 Config 进行以下简单的使用:

val d = ConfigFactory.load().getDuration("application.someTimeout")

val d = ConfigFactory.load().getDuration("application.someTimeout")

However I don't see any simple way to use the result in Scala. Certainly hope the good people of Typesafe didn't expect me to do this:

但是,我没有看到在 Scala 中使用结果的任何简单方法。当然希望 Typesafe 的好人没想到我会这样做:

FiniteDuration(d.getNano, TimeUnit.NANOSECONDS)

FiniteDuration(d.getNano, TimeUnit.NANOSECONDS)

Edit: Note the line has a bug, which proves the point. See the selected answer below.

编辑:请注意该行有一个错误,这证明了这一点。请参阅下面的选定答案。

回答by Gabriele Petronella

I don't know whether an explicit conversion is the only way, but if you want to do it right

我不知道显式转换是否是唯一的方法,但如果你想正确地做

FiniteDuration(d.toNanos, TimeUnit.NANOSECONDS)

toNanoswill return the total duration, while getNanowill only return the nanoseconds component, which is not what you want.

toNanos将返回总持续时间,而getNano只会返回纳秒分量,这不是您想要的。

E.g.

例如

import java.time.Duration
import jata.time.temporal.ChronoUnit
Duration.of(1, ChronoUnit.HOURS).getNano // 0
Duration.of(1, ChronoUnit.HOURS).toNanos  // 3600000000000L

That being said, you can also roll your own implicit conversion

话虽如此,您也可以推出自己的隐式转换

implicit def asFiniteDuration(d: java.time.Duration) =
  scala.concurrent.duration.Duration.fromNanos(d.toNanos)

and when you have it in scope:

当你在范围内时:

val d: FiniteDuration = ConfigFactory.load().getDuration("application.someTimeout")

回答by Tyth

I don't know any better way, but you can make it a bit shorter:

我不知道有什么更好的方法,但你可以把它缩短一点:

Duration.fromNanos(d.toNanos)

and also wrap it into an implicit conversion yourself

并自己将其包装成隐式转换

implicit def toFiniteDuration(d: java.time.Duration): FiniteDuration = Duration.fromNanos(d.toNanos)

(changed d.toNanoto d.toNanos)

(变更d.toNanod.toNanos

回答by Xavier Guihot

Starting Scala 2.13, there is a dedicated DurationConverterfrom java's Durationto scala's FiniteDuration(and vice versa):

开始Scala 2.13,有一个DurationConverter从 javaDuration到 Scala 的专用FiniteDuration(反之亦然):

import scala.jdk.DurationConverters._

// val javaDuration = java.time.Duration.ofNanos(123456)
javaDuration.toScala
// scala.concurrent.duration.FiniteDuration = 123456 nanoseconds

回答by Stephen

There is a function for this in scala-java8-compat

有一个功能 scala-java8-compat

in build.sbt

在 build.sbt

libraryDependencies += "org.scala-lang.modules" %% "scala-java8-compat" % "0.9.0"
import scala.compat.java8.DurationConverters._

val javaDuration: java.time.Duration = ???
val scalaDuration: FiniteDuration = javaDuration.toScala