implicit conversion from LocalDate (LocalDateTime) to Timestamp

176 views
Skip to first unread message

Sanjarbek Amatov

unread,
Dec 16, 2014, 6:42:58 AM12/16/14
to squ...@googlegroups.com
Hi to all.

I am using LocalDate in my case classes. To work correctly I am using implicit defs
class MyLocalDate(d: Date) extends DateField(d) {   
  }

  implicit def myLocalDateToLocalDate(date: MyLocalDate): LocalDate = {
    val instant = Instant.ofEpochMilli(date.value.getTime)
    LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).toLocalDate
  }

  implicit def optionMyLocalDateToLocalDate(date: Option[MyLocalDate]): Option[LocalDate] = {
    date.map{tmp =>
      val instant = Instant.ofEpochMilli(tmp.value.getTime)
      Some(LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).toLocalDate)
    }.getOrElse(None)
  }

  implicit def localDateToMyLocalDate(date: LocalDate): MyLocalDate = {
    val instant = date.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()
    new MyLocalDate(Date.from(instant))
  }

  implicit def optionLocalDateToMyLocalDate(date: Option[LocalDate]): Option[MyLocalDate] = {
    date.map{tmp =>
      val instant = tmp.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()
      Some(new MyLocalDate(Date.from(instant)))
    }.getOrElse(None)
  }

If class field is just LocalDate it is working, but if Option[LocalDate] it is not working. 
How to make implicit conversion of Option[LocalDate] field???

Any help would be appreciated.

David Whittaker

unread,
Dec 16, 2014, 8:36:17 AM12/16/14
to squ...@googlegroups.com
Are you using Lift Record here?  Can you give an example of where you want the conversion to be applied?

--
You received this message because you are subscribed to the Google Groups "Squeryl" group.
To unsubscribe from this group and stop receiving emails from it, send an email to squeryl+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sanjarbek Amatov

unread,
Dec 16, 2014, 6:40:28 PM12/16/14
to squ...@googlegroups.com
No, I am using play framework.
case class Employee(
                     id: Long,
                     surname: String,
                     firstname: String,
                     lastname: String,
                     birthday: Option[MyLocalDate],
                     citizenship: String,
                     insurance_number: String,
                     tax_number: String,
                     sex: Boolean,
                     @Column("relationship_status_id") val relationshipStatus: Int,
                     @Column("nationality_id") val nationalityId: Int,
                     override var created_at: TimeStamp,
                     override var updated_at: TimeStamp
                     ) extends Entity[Long] {
  override def save = inTransaction {
    super.save.asInstanceOf[Employee]
  }

  def update = inTransaction {
    Employee.findById(this.id).map { employee =>
      val tmp = this.copy(created_at = employee.created_at, updated_at = employee.updated_at)
      tmp.save
    }
  }
}

birthday of Employee class is Option[MyLocalDate].


вторник, 16 декабря 2014 г., 19:36:17 UTC+6 пользователь David Whittaker написал:

Sanjarbek Amatov

unread,
Dec 16, 2014, 6:45:45 PM12/16/14
to squ...@googlegroups.com
I have a run time error in code
val tmp = Employee(0,1, 1, .........,None (here is field Option(MyLocalDate),....).save



вторник, 16 декабря 2014 г., 19:36:17 UTC+6 пользователь David Whittaker написал:
Are you using Lift Record here?  Can you give an example of where you want the conversion to be applied?

David Whittaker

unread,
Dec 17, 2014, 1:59:55 PM12/17/14
to squ...@googlegroups.com
I'm still not following what the problem is.  Please post the full call that is generating the error, and what the error consists of.

Sanjarbek Amatov

unread,
Dec 21, 2014, 9:16:36 AM12/21/14
to squ...@googlegroups.com
For now I return to using java.util.Date class. But question was how to implicitly convert custom type to squeryl type with Option.

четверг, 18 декабря 2014 г., 0:59:55 UTC+6 пользователь David Whittaker написал:

David Whittaker

unread,
Dec 22, 2014, 1:17:31 PM12/22/14
to squ...@googlegroups.com
I understand that much.  If you can't post the code you are using and the error that your'e receiving, as I requested, I don't think I can help you.

Sanjarbek Amatov

unread,
Dec 23, 2014, 9:03:13 AM12/23/14
to squ...@googlegroups.com
At the moment I can't reproduce it. I will post when it will possible. Thank you.
Reply all
Reply to author
Forward
0 new messages