Skip to content

Instantly share code, notes, and snippets.

@bdkosher
Created February 25, 2014 22:46
Show Gist options
  • Save bdkosher/9219616 to your computer and use it in GitHub Desktop.
Save bdkosher/9219616 to your computer and use it in GitHub Desktop.
Extends groovy.time.TimeCategory with the ability to cast Strings into Dates.
class EasyDateCategory extends groovy.time.TimeCategory {
static asType(String str, Class clazz) {
if (clazz == Date) {
new java.text.SimpleDateFormat('yyyy-MM-dd').parse(str)
} else {
str.asType(clazz)
}
}
}
use(EasyDateCategory) {
println(('2014-02-25' as Date) + 3.months)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment