Converting Java to Scala durations

Starting Scala 2.13, there is a dedicated DurationConverter from java's Duration to scala's FiniteDuration (and vice versa):

import scala.jdk.DurationConverters._

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

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

in 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

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.toNano to d.toNanos)


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)

toNanos will return the total duration, while getNano will only return the nanoseconds component, which is not what you want.

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")