Why does new Date(year:x) add 1900 to x ?

1,002 views
Skip to first unread message

Andy Miller

unread,
Dec 29, 2010, 6:24:18 PM12/29/10
to Groovy Users of Minnesota, john....@objectpartners.com
I'm stumped. Can anyone explain this...

groovy> println "${ new Date(year:2010) }"
groovy> println "${ new Date(year:110) }"

Thu Dec 29 17:16:29 CST 3910
Wed Dec 29 17:16:29 CST 2010

Am I destined, even with groovy, to fight forever with the mystical
guts of the old Taligent Calendar?

Ted Naleid

unread,
Dec 30, 2010, 1:17:24 AM12/30/10
to groo...@googlegroups.com, john....@objectpartners.com
Don't blame groovy, this is Java setting the epoch time for the year it stores to start in 1900:


The year setter that's getting called by the map you're passing to the constructor is deprecated (and has been since Java 1.1).  The javadocs suggest using Calendar.set(Calendar.YEAR, year + 1900), but I'd suggest using JodaTime and forgetting about the java date/calendar ugliness if at all possible.

-Ted


--
You received this message because you are subscribed to the "Groovy Users of Minnesota" group.

To post to this group, send email to groo...@googlegroups.com
To unsubscribe from this group, send email to groovymn-u...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/groovymn?hl=en

Luke Bredeson

unread,
Dec 30, 2010, 1:17:52 AM12/30/10
to groo...@googlegroups.com, john....@objectpartners.com
I'm guessing it's because the map you passed to the constructor is converted by Groovy to a deprecated setYear(int year) method call on java.util.Date that adds 1900 to the parameter you passed.  See:  http://download.oracle.com/javase/6/docs/api/java/util/Date.html#setYear(int)

Here's a small example:

class MyDate {
    int date
    def setYear(int year) {
        this.date = year + 1900
    }
}
println new MyDate(year: 110).date

I wish I could explain why the deprecated method adds 1900 to begin with, or why something deprecated since 1.1 is still hanging around...

Luke

On Wed, Dec 29, 2010 at 5:24 PM, Andy Miller <mil...@visi.com> wrote:

Andy Miller

unread,
Dec 30, 2010, 11:32:35 AM12/30/10
to groo...@googlegroups.com
Thanks Ted!
JodaTime is definitely better.
And though it seems so simple now, I hadn't thought to look up setYear
in the date javadoc.

Hope all is well at Bloom,
Andy.


On 12/30/10 12:17 AM, Ted Naleid wrote:
> Don't blame groovy, this is Java setting the epoch time for the year
> it stores to start in 1900:
>
> http://download.oracle.com/javase/1.4.2/docs/api/java/util/Date.html#setYear(int)

> <http://download.oracle.com/javase/1.4.2/docs/api/java/util/Date.html#setYear%28int%29>

Andy Miller

unread,
Dec 30, 2010, 12:52:44 PM12/30/10
to groo...@googlegroups.com

> ...why something deprecated since 1.1 is still hanging around...

I suppose this is a side effect of the dynamic nature of groovy:
Accessing methods in this way means there's no tool support for warning
me that the code is using methods marked as deprecated in java.

Andy.


On 12/30/10 12:17 AM, Luke Bredeson wrote:
> I'm guessing it's because the map you passed to the constructor is
> converted by Groovy to a deprecated setYear(int year) method call on
> java.util.Date that adds 1900 to the parameter you passed. See:
> http://download.oracle.com/javase/6/docs/api/java/util/Date.html#setYear(int)

> <http://download.oracle.com/javase/6/docs/api/java/util/Date.html#setYear%28int%29>

Reply all
Reply to author
Forward
0 new messages