Dark theme | Light theme

December 16, 2010

Groovy Goodness: Convert Date to Calendar

In Groovy version 1.7.6 we can convert a Date to a Calendar with the toCalendar() method. The toCalendar() method is added to the Date class by Groovy.

import static java.util.Calendar.*

def date = new Date()
date.set year: 2010, month: 11, date: 16

def calendar = date.toCalendar()

assert calendar[YEAR] == 2010
assert calendar[MONTH] == Calendar.DECEMBER
assert calendar[DATE] == 16
assert calendar.format('dd-MM-yyyy') == '16-12-2010'
assert calendar in Calendar