ArunavaS ArunavaS - 1 month ago 8
Scala Question

How to convert from Timestamp to com.websudos.phantom.dsl.DateTime or set the current time

I am using phantom 1.29.4 and scala 2.11.8, trying to do hands on cassandra with scala. I have my datamodel like below...

case class User(id: Long, name: String, createdDate: Timestamp, ...)
class UserTableMapping extends CassandraTable[UserTableDao, User] {
object createdDate extends DateTimeColumn(this)

abstract class UserTableDao extends UserTableMapping with RootConnector {
def createUser(user: User) = insert.value...(_.createdDate, user.createdDate)

Now, I am getting a type mismatch error ("expected com.websudos.phantom.dsl.DateTime actual java.sql.Timestamp" which is obvious)... Now my question is how do I convert Timestamp to DateTime (because, I have my service layer in different sub project and I don want to add all phantom dsl jars there) or provide current time to Datetime?

I have also tried providing a implicit conversion like below...

implicit def sqlTimestampToPhantomDateTime(dt: Timestamp): DateTime = new DateTime(dt)

but still no luck...

Please help me guys as I am new to cassandra... Thanks...


Thats just a joda DateTime:

type DateTime = org.joda.time.DateTime

The joda DateTime has a millisecond constructor, so you're almost there. All you need to do is get the timestamp in millis from your Timestamp instance and use it to construct a DateTime instance:

new DateTime(timestampInstance.getTime)

However you can also just create a new DateTime instance with the no args constructor to have the current time:

new DateTime()