Groovy Groundhogs

So the groundhog saw his shadow today, implying six more weeks of Winter.  Let’s settle this nonsense once and for all.

def cal = Calendar.instance
cal.set(2009,Calendar.FEBRUARY,2)
def ghday = cal.time
cal.set(2009,Calendar.MARCH,21)
def spring = cal.time
def days = (ghday..spring).size()
println "Between Feb 2 and Mar 21, there are $days days"

The output is

Between Feb 2 and Mar 21, there are 48 days

In other words, there are six weeks and six days between Groundhog Day and the first day of Spring.  So we’re already going to have an early Spring. 🙂

About Ken Kousen
I teach software development training courses. I specialize in all areas of Java and XML, from EJB3 to web services to open source projects like Spring, Hibernate, Groovy, and Grails. Find me on Google+ I am the author of "Making Java Groovy", a Java / Groovy integration book published by Manning in the Fall of 2013, and "Gradle Recipes for Android", published by O'Reilly in 2015.

5 Responses to Groovy Groundhogs

  1. jcadawell says:

    nice tips.

    btw, you should try joda-time. it is very nice to play with date.

  2. Ken Kousen says:

    Yes, I’m familiar with joda time, but for something this simple it was easy enough to use the regular Calendar class.

  3. Steve says:

    lol! And I learned something about date ranges, too. Thanks!

  4. adwin says:

    i am interested with this date range

     def days = (ghday..spring).size() 

    can you tell me what kind of object that has “range” features .. I tought it was only applied to Integer but now you showed us that it can be applied to java.util.Date object. that’s amazing.

  5. Pablo says:

    I live in the southern hemisphere, so it’s summer here, but I liked how you played with calendars anyway…

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: