I have a question, how to check in groovy, if the the date older than 5 days comparing with now.
def now = new Date() : Wed Feb 23 11:05:06 CET 2022
String testDate = "2002-2-10"
I have 2 input dates : Fri Feb 11 17:17:42 2022 0100
and Wed Feb 23 11:05:06 CET 2022
How can I check, if testDate oder than 5 days?
any solution?
CodePudding user response:
You can use a calculation like this on a Date object:
Date fiveDaysAgo = new Date().minus(5)
You will have to get the Date object from your String like this:
Date testDateDate = Date.parse("yyyy-MM-dd", testDate)
Be careful, in your example you have "yyyy-MM-dd"
and "2002-2-10"
and they are not the same format.
CodePudding user response:
You can check for difference in days like so:
Date minus5 = new Date() - 5
Beware though, that in order to do this and other date-related ops like formatting or parsing, you have to add https://mvnrepository.com/artifact/org.codehaus.groovy/groovy-dateutil to your dependencies.
If you cann't do that, you can use good-ol milliseconds based approach:
Date minus5 = new Date( System.currentTimeMillis() - 5l * 24 * 60 * 60 * 1000 )