0

I have some date in format java.sql.Date and I want to compare them

import java.sql.Date
import org.apache.spark.sql.types.{DateType, IntegerType}
var a = Date.valueOf("2018-11-09")
var b = Date.valueOf("2019-11-09")

a: java.sql.Date = 2018-11-09
b: java.sql.Date = 2019-11-09

If I compare with an equal sign it work

a == b
res1: Boolean = false

But if I want to know which one is bigger than the other it return an error:

 a >= b
<console>:37: error: value > is not a member of java.sql.Date

I would expect it to return false.

How can I compare a and b.?

2 Answers 2

4

You may use java.sql.Date#compareTo:

var a = Date.valueOf("2018-11-09")
var b = Date.valueOf("2019-11-09")

if (b.compareTo(a) > 0) {
    println("Date b is later than date a.");
}
Sign up to request clarification or add additional context in comments.

Comments

3

If you want to use the comparison operators such as a >= b instead of compareTo, you can: just add

import scala.math.Ordering.Implicits._

for Comparable types. java.sql.Date is actually a bit unusual, because it's Comparable with java.util.Date and not java.sql.Date. So for this type you need type ascription:

(a: java.util.Date) >= b

3 Comments

OK, edited (I started writing my answer before yours appeared).
Hi Alexey Romanov, thank you for your answer however it does not work with me..... I do the import "import scala.math.Ordering.Implicits._" but when I tried a>= b I still get the same error...
@Anneso Edited it.

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.