I have a validFrom and validTo date field in my domain class in grails project. I want to create a service on the basis of its validation. can anyone pls tell me how to check if it is greater or lesser than current system date?
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.
Groovy - compareTo() The compareTo method is to use compare one number against another. This is useful if you want to compare the value of numbers.
You can't compare just any date string. For instance, "13-Dec-2020" < "20-Apr-2020" alphabetically but not conceptually. But ISO date strings are neatly comparable, for instance, "2020-12-13" > "2020-04-20" both conceptually and alphabetically.
It's
Date now = new Date()
if (validFrom.before(now) && validTo.after(now)) {
//valid for this moment
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With