John Goering
John Goering

Reputation: 39030

Best way to check whether two dates are the same calendar day in Groovy?

Obviously I could create a Calendar object with the date and use get(DAY) on it, but knowing Groovy, I would guess there is an easier, quicker way I just don't know about?

Answer

(date1..date2).size() == 1 // true if two dates are on same calendar day

Upvotes: 5

Views: 2178

Answers (2)

Eli Willaert
Eli Willaert

Reputation: 11

A bit shorter and more readable then the current top answer:

date1 - date2 == 0 //true if two dates are on same calendar day

Upvotes: 1

Ruben
Ruben

Reputation: 9120

Quickly opened the Groovy In Action book and found the following sample that should help:

def today = new Date()
def yesterday = today-1
assert (yesterday..today).size() == 2

Upvotes: 5

Related Questions