Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compare date greater than equal in scala

Tags:

scala

Scenario 1

val date1 =  LocalDate.parse("2017-02-07")
val date2 =LocalDate.parse("2017-02-01")
date1.isAfter(date2)

Output

true

Scenerio 2

val date1 =  LocalDate.parse("2017-02-07")
val date2 =LocalDate.parse("2017-02-07")
date1.isAfter(date2)

Output

false

I want to return true when date is > = "2017-02-07"

like image 319
coder25 Avatar asked Oct 26 '17 07:10

coder25


People also ask

How to compare two dates are equal?

For comparing the two dates, we have used the compareTo() method. If both dates are equal it prints Both dates are equal. If date1 is greater than date2, it prints Date 1 comes after Date 2. If date1 is smaller than date2, it prints Date 1 comes after Date 2.

How to compare two date format?

equals(): Syntax: date1. equals(date2) Return Type: Boolean This function returns true strictly if date1 and date2 are equal, else it returns false. The equals() method of Date class checks for equality by comparing the number of milliseconds elapsed since 1st January 1970, 00:00:00 GTM.

How to compare date objects?

The date object allows us to perform comparisons using the > , < , = , or >= comparison operators, but not the equality comparison operators like == , != , === , and !== (unless we attach date methods to the date Object).


1 Answers

Use an OR. afaik, there is no method in the API to do that in a single operation

date1.isAfter(date2) || date1.isEqual(date2)

Another option:

date1.compareTo(date2)  >=0 
like image 89
SCouto Avatar answered Oct 10 '22 17:10

SCouto