Groovy Groundhogs (again)

Last year on Groundhog Day I posted a short Groovy script about the consequences of the groundhog seeing his shadow. I wasn’t going to do it again, but then I thought about the movie and realized I had to.

Here’s the script, which I’ll explain afterwards.

println 'Groundhog sees shadow --> 6 more weeks of Winter'
def c = Calendar.instance
c.set 2011, Calendar.FEBRUARY, 2
def groundHogDay = c.time
c.set 2011, Calendar.MARCH, 20
def firstDayOfSpring = c.time
def days = firstDayOfSpring - groundHogDay
assert days == (firstDayOfSpring..groundHogDay).size() - 1
println """
There are ${(int)(days/7)} weeks and ${days % 7} days between GroundHog Day 
and the first day of Spring (March 21), so Spring 
comes early if the groundhog sees his shadow.

Date and time manipulation in Java has always been ugly and awkward. Groovy doesn’t really fix that, but at least it requires fewer lines of code. By accessing the instance property on java.util.Calendar, I wind up invoking the getInstance() factory method. Calendar has a set method that takes three arguments: the year, the month, and the day. Here I left out the optional parentheses, and made sure to use the month constants because the integer values have an off-by-one problem (January = 0, etc).

Just to remind you you’re dealing with a dumb API, the way to extract a Date from a Calendar is to call getTime, so I accessed the time property to do that.

So basically I generated a java.util.Date for Groundhog Day, and a java.util.Date for the vernal equinox for this year (i.e., the first day of Spring). All that was essentially just Java. Then comes the cool part. Groovy adds a minus method to Date, so I can subtract dates and get the number of days in between. I did that, and verified the calculation by also using the dates in a range and asking its size() method and again correcting for any off-by-one errors.

(Bad joke warning: Java arrays have a length attribute, Strings have a length() method, collections have a size() method, NodeLists have a getLength() method, and so on. In Groovy, you just use size() everywhere and it works. In other words, in Groovy, size matters.

Sorry about that. Now back to your blog post, already in progress.)

The bottom line is that in 2011, there are six weeks and four days between Groundhog Day and the first day of Spring. Therefore, if Punxutawney Phil were to see his shadow, we would then expect six more weeks of winter. That’s a good thing — Spring will be four days early.

It’s a silly demo to be sure, but it’s easy enough to do in Groovy.

Two points are worth noting. First, Phil didn’t see his shadow this year (an unusual occurrence), so we are supposed to have an early Spring. Good. Second, according to the Wikipedia page on Groundhog Day, before the Gregorian calendar messed it up the equinox fell on March 16, which is exactly six weeks after February 2.

To go off on a tangent, did you know that the switch from Julian to Gregorian calendars actually impacted U.S. history? That seems unlikely, given that Pope Gregory signed the relevant decree in 1582, long before the Europeans started settling here in big numbers. However, Pope Gregory was a contemporary of Henry VIII, who engineered the separation of the Church of England, so Britain wasn’t going to do anything just because the Pope said so. Britain (and therefore the British Empire) didn’t formally adopt the Gregorian calendar until 1752. As a result, the American colonies dropped 11 days from September of that year.

In a book I read about calendaring (a fascinating subject, by the way) about 20 years ago (sigh), I seem to remember the author making a big deal out of how 11 days were removed from February and that George Washington lost a birthday that year. Five minutes of research on the web showed me that the lost days were in September. And they say the Internet hasn’t made life better…

Incidentally, Groundhog Day is one of the few movies actually available in the Netflix instant queue (which has almost nothing else that I search for, unfortunately), so I could actually watch it again tonight assuming the web access in my hotel room in Austin, TX could handle it. I’ve seen it so many times, though, that maybe I’ll actually work on my book rather than doing so.

What book, you say? Why, Making Java Groovy, available now through the Manning Early Access Program, which will contain the above example, among others. 🙂

About Ken Kousen
I am a Java Champion and the author of the books "Modern Java Recipes" (O'Reilly Media), "Gradle Recipes for Android" (O'Reilly Media), and "Making Java Groovy" (Manning), as well as over a dozen video courses at Safari Books Online. I'm a regular member of the No Fluff, Just Stuff conference tour and have given talks all over the world. Through my company, Kousen IT, Inc, I've taught training courses to and worked with thousands of developers.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: