I recently came across a new tidbit of info while working with the Date class in the GDK and I thought I would point it out real quick. I wanted a easy way to compare two Date objects while disregarding time. One to represent today’s date and the other to represent a future date. So I took a look at the GDK docs for the Date class and I noticed that since version 1.5.7, Groovys’ Date now has a static parse(..) method and a instance format(..) method which worked out nice for me in terms of writing less code. Here is a snippet of my code:
def myFormat = 'MM/dd/yyyy'
if Date.parse(myFormat, '02/03/2009') >= Date.parse(myFormat, new Date().format('MM/dd/yyyy')) {...}If you did Date comparisons using regular Java, you would know that you couldn’t get away with something like this. I’m loving Groovy more and more everyday.
If you have an even shorter or more succinct way of coding this simple comparison, please share it.
Tags: Groovy
| M | T | W | T | F | S | S |
|---|---|---|---|---|---|---|
| « Jun | ||||||
| 1 | 2 | 3 | 4 | 5 | ||
| 6 | 7 | 8 | 9 | 10 | 11 | 12 |
| 13 | 14 | 15 | 16 | 17 | 18 | 19 |
| 20 | 21 | 22 | 23 | 24 | 25 | 26 |
| 27 | 28 | 29 | 30 | |||