Search code examples
javascalajodatimenscala-time

Convert nscala-time Datetime to java.util.Date


I am trying to cast com.github.nscala_time.time.Imports.DateTime from nscala-time (a wrapper of joda-time) to java.util.Date

activeUntil.toDate()

But I get this error

value toDate is not a member of Option[com.github.nscala_time.time.Imports.DateTime]

Obviuosly is not the right way to do it. Is there a way to do this?

Thank you in advance


Solution

  • Apparently activeUntil is an Option[DateTime] not DateTime itself. You can map it to Date and get providing some default value in case Option is empty like this

    activeUntil.map(_.toDate).getOrElse(new Date())
    

    I am not sure where did you get activeUntil from, but probably from a method that can fail to give you your DateTime this is why it returned an Option and forced you to handle the case when there is nothing to return.