0

I have a Scala class that follows:

 case class Game(id: Option[String], ....... start: Option[java.sql.Timestamp], end: Option[java.sql.Timestamp], ..)

This class is used by slick to save the games objects into the DB. Start and End are used to represent when they started and ended. There is also a Games class that works like the plural for Game.

I created a method that looks like

def getAllGamesBetween(iniDate: Option[String], endDate:Option[String]) = Action.async { request => 
      for {
             lGames <- Games.getAll(g => iniDate.get < Timestamp.valueOf(g.start) && Timestamp.valueOf(g.start) < endDate)

      } yield {
         Ok(.....);
     }  

}

The problem I have is that I need to understand how to harmonize the values obtained through iniDate:Option[String] and endDate:Option[String] and their respective field values

  start: Option[java.sql.Timestamp]
  end: Option[java.sql.Timestamp]

so I can make comparisons and see, for example, which one is before another or after another.

One more thing, iniDate and endDate will get Option[string] values that follow the format: Option["YYYY-MM-DD hh:mm:ss"]. Similarly the values for start and end are stored in the DB as "YYYY-MM-DD hh:mm:ss"

2 Answers 2

1

you need to first parse your String dates to Timestamps so you can actually compare them, using something like this:

def parseDateString(s:String) :java.sql.Timestamp = {
  val d = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(s)
  new java.sql.Timestamp(d.getTime())
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can use after and before methods of Timestamp.

for example: Consider you want to check and validate the start and end date and the start and end date are coming in type java.sql.Timestamp

You can pass any string in format yyyy-mm-dd hh:mm:ss[.fffffffff]

def isValidTimestamp(gpsTimestamp: Timestamp): Boolean = {
return endTimestamp.before(Timestamp.valueOf("1970-03-21 18:40:47.000000")) 
&&
  startTimestamp.after(Timestamp.valueOf("1970-03-21 18:40:47.000000"))
}

Comments

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.