2

I have a slick column that is a Rep[Option[Timestamp]]. I'd like to compare it to a user supplied java.util.LocalDate via the filter method. I'm only interested in comparing the date (ddMMyyyy), not the time:

val userSuppliedLocalDate: LocalDate = ...
usersTable.filter(_.modifiedDate === userSuppliedLocalDate).result // Doesn't compile

I thought about converting the Rep[Timestamp] (_.modifiedDate) to a LocalDate, but unsure if this is possible (unwrapping a Rep type?)

Or, converting the userSuppliedLocalDate to a java.sql.Timestamp and doing a comparison ignoring the time portions - is this possible?

1 Answer 1

4

Not recommended way

 Warning: Take care of time zone information

You can convert LocalDate into timestamp and compare the timestamp.

val timestamp = Timestamp.valueOf(localDate.atStartOfDay)

Your method becomes

val userSuppliedLocalDate: LocalDate = ...
val userSuppliedTimestamp = Timestamp.valueOf(localDate.atStartOfDay)
usersTable.filter(_.modifiedDate === userSuppliedTimestamp).result

But, this is not the recommended way.

If you want to compare timestamp of particular day. One Hack would be

val start = Timestamp.valueOf(localDate.atStartOfDay())
val end = Timestamp.valueOf(start.plusDays(1))

usersTable.filter(row => row.modifiedDate >= start && row.modifiedDate < end).result

Recommended way is to use LocalDate itself as slick table column.

How is this possible?

This possible with slick column mapper.

Have below implicit in the scope.

implicit val mapper = MappedColumnType.base[LocalDate, Timestamp](
 _.toLocalDateTime().toLocalDate(),
 Timestamp.valueOf)

Now you can have a LocalDate column directly

def birthday = column[LocalDate]("birthday")

Now you can directly compare

def birthdaysBefore(localDate: LocalDate) = 
  query.filter(_.birthday < localDate).result

You can use other comparison operators as well.

Sign up to request clarification or add additional context in comments.

3 Comments

For the 'Not recommended way', I want to ignore the times, is this possible? (I only want to compare the dates)
Thanks, what about _.modifiedDate - do we need to change this to atStartOfDay as well (otherwise the times might be different?). Although not sure I can modify it via the filter function
@Rory Added more to the answer. please check

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.