0

事件类定义

  class event extends MongoRecord[event] with MongoId[event]{
      def meta = event
      object title extends StringField(this, 70)
      object description extends StringField(this, 200)
      object date_time extends DateTimeField(this)
      object email extends StringField(this, 70)
      object placeName extends StringField(this, 70)
      object placeLat extends DoubleField(this) 
      object placeLon extends DoubleField(this) 
      object placeCity extends StringField(this, 100)


      }
object event extends event with MongoMetaRecord[event] 

从文本中存储日期:

val event_temp = event.createRecord
val cal = Calendar.getInstance()
val dashDate = new SimpleDateFormat("yyyy/MM/dd HH:mm")
cal.setTime(dashDate.parse(String))
event_temp.date_time(cal)
event_temp.save

获取事件和排序

val events= event.findAll
events.sortWith(_.date_time.is before _.date_time.is)

events 是 List[event],而 date_time.is 返回 java.util.Calendar。它返回未排序的 List[event]。

4

1 回答 1

1

我认为 Scala 2.9.1 没有问题:

scala> def d(y:Int) = { val c = Calendar.getInstance; c.setTime(new Date(y,0,0)); c }
warning: there were 1 deprecation warnings; re-run with -deprecation for details
d: (y: Int)java.util.Calendar

scala> val cs = d(100) :: d(1) :: d(1000) :: d(150) :: Nil 
// List[java.util.Calendar] .. removed long and useless

scala> val cs2 = cs sortWith { _ before _ } 
// List[java.util.Calendar] .. removed long and useless

scala> cs map { _.getTimeInMillis }
res21: List[Long] = List(946605600000, -2177528012000, 29347927200000, 2524528800000)

scala> cs2 map { _.getTimeInMillis }
res22: List[Long] = List(-2177528012000, 946605600000, 2524528800000, 29347927200000)
于 2012-09-28T19:33:21.043 回答