0

I wanted to know if it is possible to use java.time package in java 8 in my scala 2.11 program?

then how to add it to my sbt project?

5
  • If you're compiling with java 8 you should be able to "just use" it. Don't think you need to add anything to SBT. Did you try that? Commented Sep 5, 2016 at 10:38
  • Possible duplicate of How to force SBT to use Java 8?
    – asachet
    Commented Sep 5, 2016 at 10:59
  • @antoine-sac No, it is not a duplicate of that. This question clearly shows that the OP is a scala and sbt beginner who is trying to understand the use of Java libs in sbt projects. Commented Sep 5, 2016 at 11:04
  • @SarveshKumarSingh Right, I got misled by the answer.
    – asachet
    Commented Sep 5, 2016 at 11:06
  • As @DenisRosca mentioned, if you are comipling it with jdk 8 then you can just import it in your code. Commented Sep 5, 2016 at 11:11

2 Answers 2

4

You don't need to add anything to your project. java.time is a standard part of Java, not an external third-party dependency.

You just need to make sure that your customers run your code on Java 8.

This has nothing to do with SBT. SBT is a Build Tool. It is about how you compile your code. But your problem is that you need to control how your customers run your code. You could do that simply by stating in your documentation that your code only works with Java 8+ and hope that your customers actually read that documentation. You could use a launcher that checks the Java version before running the main code and prints out a suitable error message.

2
  • Hi, I am using scala 2.11.5 but can't import java.time..Is this supported by my scala version?
    – Mahdi
    Commented Sep 5, 2016 at 23:30
  • 2
    java.time is a part of Java. It has nothing whatsoever to do with Scala. It even has "Java" in its name! Your Scala version is completely and utterly irrelevant. If you have Java 8, you have java.time. If you don't have Java 8, you can't use it. It doesn't matter if you are using Scala 2.11.5, Scala 2.11.8, Scala 2.12, Scala 1.0, Clojure, Groovy, Kotlin, Ceylon, Fantom, Ruby, Python, JavaScript, PHP, Scheme, CommonLisp, Java, or any other language. Commented Sep 5, 2016 at 23:33
-2

Until Scala 2.12 comes along, Java 6, 7, or 8 can be used with any of its inbuilt packages and java.time is an inbuilt package.So can be used. However newest release Scala 2.12, will support Java 8 only.So any case yes it can be used in Java 8.

If you explicitly want this package in Scala you have to add Java.time dependency to sbt file.On maven central its replaced by Joda-time.Its an individual Java.time Api provided by Apache.

It can be found at https://mvnrepository.com/artifact/joda-time/joda-time

To use it add:

(
libraryDependencies += Seq( "joda-time" % "joda-time"    % "2.3"
                           , "org.joda"  % "joda-convert" % "1.6"
 // other dependencies 
                           ) 

to you sbt file.

joda-convert is needed for Scala compilation of Joda-time.It actually converts objects to and from Strings.

NOTE:If you explicity want to use java.time Scala implementation find Scal-Java-time link on Github.

0

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