62

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

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

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:

FiniteDuration(d.getNano, TimeUnit.NANOSECONDS)

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

4
  • What is wrong with the shown solution? This is easy and understandable. Wrap it in a function/create an implicit conversion if it is not short enough for you.
    – kiritsuku
    Aug 18, 2015 at 15:32
  • @sschaef the bothering bit is a supposedy "scala-friendly" library to spit out Java types, but this isn't a new thing when speaking of Config. Aug 18, 2015 at 15:34
  • Typesafe Config is serving both Java and Scala clients, by its project definition. I agree that having to use an implicit conversion is a bit surprising. I cover this within the MyConfig singleton, so the application code will never need to know. One should not sprinkle raw Typesafe Config accesses in application level code.
    – akauppi
    Feb 11, 2016 at 13:52
  • For some use, reading the config as certain units (normally ms) might be sufficient. Then simply '.toMillis' will do the trick. This still allows the configuration files to provide any unit that is nice for us humans.
    – akauppi
    Mar 15, 2016 at 15:20

4 Answers 4

56

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
53

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

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)

8

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.