Loading...

June 14, 2010

Groovy Goodness: Setting Date and Calendar Values with Subscript Operators

In Groovy 1.7.3 we have some new ways to set the value of Date or Calendar objects. We can for example use the subscript operators (getAt() and putAt() methods) to define value for fields or get the value from a field. The fields are the Calendar constants like Calendar.YEAR, Calendar.DATE.

import static java.util.Calendar.*

def date = new Date()
// Set value with subscript operator
date[YEAR] = 2010
date[MONTH] = JUNE
date[DATE] = 14

assert 110 == date.year
assert 5 == date.month
assert 14 == date.date

// Get value with subscript operator
assert 2010 == date[YEAR]  
assert JUNE == date[MONTH]
assert 14 == date[DATE]


def cal = Calendar.instance
// Set value with subscript operator
cal[YEAR] = 2000
cal[MONTH] = DECEMBER
cal[DATE] = 25

assert '2000-12-25' == cal.format('yyyy-MM-dd')
assert 2000 == cal[YEAR]  // Get value with subscript operator