Tuesday, April 30, 2013

Method updated() is not setting hours, minutes and seconds in Groovy Date class

Recently, I wanted to update existing date in Groovy like:


> new Date().updated(year: 2013, month: Calendar.APRIL, date: 15, hours: 0, minutes: 0, seconds: 0)
Mon Apr 15 21:59:01 EEST 2013

It sets date, month and year perfectly, but completely ignores hours, minutes and seconds.
After checking Groovy sources I found that these are incorrect property names, and correct code is:


> new Date().updated(year: 2013, month: Calendar.APRIL, date: 15, hourOfDay: 0, minute: 0, second: 0)
Mon Apr 15 00:00:00 EEST 2013

First thing is, that it is not mentioned in GroovyDoc, but what adds confusion is that Intellij IDEA autocomplets variables in this way, because Date has deprecated setters with such property names.

No comments:

Post a Comment