Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to compare a date with current date in groovy

Tags:

grails

groovy

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?

like image 380
Sanjib Karmakar Avatar asked May 10 '12 14:05

Sanjib Karmakar


People also ask

How do you compare two different dates?

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 do I compare two values in groovy?

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.

Can you compare two dates as strings?

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.


1 Answers

It's

Date now = new Date()
if (validFrom.before(now) && validTo.after(now)) {
  //valid for this moment
}
like image 76
Igor Artamonov Avatar answered Oct 03 '22 02:10

Igor Artamonov