Ranges with dates (in Groovy)

During my previous ramblings with Groovy I didn't touch anything fancy in the Date/Calendar API; no need for it. But after reading a lengthy blogpost on the topic I just needed to have a look at it.

Consider the following code:

GROOVY:
  1. sdf = new java.text.SimpleDateFormat("dd-MM-yyyy")
  2.  
  3. bd_daan = sdf.parse("23-01-1980")   
  4. bd_peter = sdf.parse("18-01-1979")
  5.  
  6. t = Calendar.instance 
  7. all_sundays = (bd_peter..bd_daan).findAll{
  8.     t.time = it;
  9.     t.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY
  10. }
  11.  
  12. all_sundays.each{
  13.     println sdf.format(it)
  14. }

It prints a list of all Sundays between my and my wifes' birthday. Most of the 'magic' is done by the range operator (..) on which one can call all collection operations.

I somehow expected a more groovy like interface to the Calendar object; t.get( xxx ) just doesn't feel Groovy. Might be a nice little project to use method missing to make calls like t.dayOfWeek to work... maybe tomorrow ;)


9 Responses to “Ranges with dates (in Groovy)”

  1. 1 railsguru

    bd_daan = Date.parse("23-01-1980")
    bd_peter = Date.parse("18-01-1979")

    (bd_peter..bd_daan).each { |d| puts d if d.wday.zero? }

  2. 2 peter

    If you execute the code exactly as you pasted it; it wouldn't work. The Groovy code does work without any further 'import' or 'require'.

    If I execute:

    
    irb> require 'date/format'
    irb> bd_peter = Date._parse('18-01-1979')
    => {:mon=>1, :year=>1979, :mday=>18}
    irb> bd_peter = Date._parse('18-01-1979')
    => {:mon=>1, :year=>1979, :mday=>18}
    irb> bd_daan = Date._parse('23-01-1980')
    => {:mon=>1, :year=>1980, :mday=>23}
    irb> (bd_peter..bd_daan).each{|d| puts d if d.wday.zero?}
    ArgumentError: bad value for range
            from (irb):18
            from :0
    
    

    Did I import the wrong date??

    There also is a slight difference in your ruby solution; which I could have used in my example... I build an array of the results and display it in a second each loop... could have been simpler:

    
    sdf = new java.text.SimpleDateFormat("dd-MM-yyyy")
    
    bd_daan = sdf.parse("23-01-1980")
    bd_peter = sdf.parse("18-01-1979") 
    
    t = Calendar.instance
    all_sundays = (bd_peter..bd_daan).each{
    	t.time = it;
    	if(t.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
    		println sdf.format(it)
    }
    
    

    Mind you, the use of a intermediate Calendar object is a bit overkill in this case... as is the use of the full blown Java date formatter. But the advantage is: full locale awareness when needed.

  3. 3 railsguru

    The code is mend to be run in a Rails environment e.g. script/console. Anyway.. not trying to prove a point, just checked to see if the same could be done in Ruby :)

  4. 4 peter

    Aha, makes sense ;) ... I always tend to forget about the fact that Rails uses its' own date and time classes...

  5. 5 peter

    But hey, interesting... Rails' date object can actually be used in a range?

  6. 6 railsguru

    Indeed! That's the new thing I learned from this small excercise :)
    As long as your class implements and succ you can use it in Range:

    http://ruby-doc.org/core/classes/Range.html

  7. 7 railsguru

    Grr, <=> and succ that is..

  8. 8 peter

    With a little help from a colleague if figured out how to used Rails' date and time extensions when not running in Rails. Just load active_support:

    
    irb(main):005:0> require 'rubygems'
    => true
    irb(main):006:0> require 'active_support'
    => true
    
    
  1. 1 Using propertyMissing to enhance Date (in Groovy) at log4p

Leave a Reply





About

Welcome to the weblog of Peter Maas. Here you'll find various posts related to stuff I like (like my kids and espresso) and stuff I do (like developing software).

JavaOne 2008 Pictures


pub Golden Gate Scribbled Sun Logo javaone 2008 goodybag Greenland Java + You on a cab Stretched Limo Community One Keynote Stage being build in the nearby park smashmouth sea_lion Acme Anvile at CommunityOne Keynote nearby hotel Rudie alcatraz Charles Nutter & Guillaume Laforge Hotel room javaone2008 keynote Okke en Rudie Tim Bray introducing the (J)Ruby panel
View more photos >

Categories



Meld u aan voor PayPal en begin direct met het accepteren van creditcardbetalingen.