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

Scala2.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 に答える