1

I want to compare two dates if the difference between them is less than 10 minutes. How do I do that?

Date created = Date.parse('yyyy-MM-dd HH:mm:ss','2014-06-05 10:19:04')

Date now = new Date()

now.compareTo(created) gives me 1 but I want to compare the minute difference.
biniam
  • 8,099
  • 9
  • 49
  • 58
  • If you work with dates a lot I'd like to suggest you to use [JodaTime](http://www.joda.org/joda-time/) or take a look at [groovy.time](http://groovy.codehaus.org/api/groovy/time/package-summary.html) package if you use Groovy 2.2.1 – Mr. Cat Jun 06 '14 at 10:02

1 Answers1

10

Try using java.util.concurrent.TimeUnit. I'm assuming that, Groovy Date.parse creates java.util.Date, though.

def difference = now.time - created.time
if (difference < TimeUnit.MINUTES.toMillis(10)) {
    //do something
}
Psycho Punch
  • 6,418
  • 9
  • 53
  • 86