简体   繁体   中英

What to import to make < work for jodatime in Scala

I want to compare LocalDateTime but end up with an exception.

I have these classes:

import org.scala_tools.time.Imports._
import org.scala_tools.time.Implicits._

case class Event(id: String, startDate: LocalDateTime, action: String)

case class Events(events: List[Event], timeout: Int) {

   val timeoutDt: LocalDateTime =  new LocalDateTime().minusMinutes(timeout)

   lazy val oldEvents = events.filter { (e: Event) =>
     e.startDate < timeoutDt
   }

   lazy val eventsToSend = events.filterNot { (e: Event) =>
     e.startDate < timeoutDt
   }
}

I got this exception:

scala> import org.scala_tools.time.Imports._
scala> import org.scala_tools.time.Implicits._

scala> val d1 = new LocalDateTime("2010-11-17T10:10:10") 
d1: org.joda.time.LocalDateTime = 2010-11-17T10:10:10.000

scala> val d2 = new LocalDateTime("2012-11-17T10:10:10")
d2: org.joda.time.LocalDateTime = 2012-11-17T10:10:10.000

scala> val e1 = new Event("a", d1, "a")
scala> val e2 = new Event("b", d2, "b")

scala> val es = Events(List(e1, e2), 10000)

scala> val old = es.oldEvents
java.lang.NoClassDefFoundError: scala/Ordered
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.scala$tools$nsc$util$ScalaClassLoader$$super$findClass(ScalaClassLoader.scala:86)
    at scala.tools.nsc.util.ScalaClassLoader$class.findClass(ScalaClassLoader.scala:51)
    at scala.tools.nsc.util...

What did I forget?

======= EDIT ============

I am using Scala 2.8.0. Thanks to @Alex, I found that the dependency in the project/build/Project.scala file was incorrect:

import java.io.File
import sbt._

class Project(info: ProjectInfo) extends DefaultProject(info) { 
  // ... omitted code

  // OBS: Must have this to access snapshots
  val snap = ScalaToolsSnapshots

  // Time
  lazy val jodaTime = "joda-time" % "joda-time" % "1.6" 
  lazy val scalaTime = "org.scala-tools" % "time" % "2.8.0-SNAPSHOT-0.2-SNAPSHOT"  // Does not work
}

~

The code below works:

import java.io.File
import sbt._

class Project(info: ProjectInfo) extends DefaultProject(info) { 
  // ... omitted code

  // Time
  lazy val jodaTime = "joda-time" % "joda-time" % "1.6" 
  lazy val scalaTime = "org.scala-tools.time" % "time_2.8.0" % "0.2"
}

Still not sure why the snapshot didn't work.

java.lang.NoClassDefFoundError: scala/Ordered means you either have a Scala version mismatch or a classpath problem.

Most likely the library you're using that wasn't compiled for your current Scala version.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM