Monday, 17 November 2008

Groovy Awesomeness

Hey check this out:

Java:


import java.util.Calendar;
import java.util.Date;

public class PrintIndependenceDay {

public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(Calendar.MONTH, Calendar.JULY);
calendar.set(Calendar.DATE, 4);
calendar.set(Calendar.YEAR, 1776);
Date time = calendar.getTime();
System.out.println(time);
}
}


Now check the awesomeness groovy gives you:

def calendar = Calendar.instance
calendar.with {
clear()
set MONTH, JULY
set DATE, 4
set YEAR, 1776
println time
}


Here is the full article that explains everything: Getting Groovy with 'with'

rock on

2 comments:

Rob said...

Nothing using java.util.Calendar can ever be considered cool, dude.

Stephen Cresswell said...

JodaTime on the other hand...

new DateTime()
.withDayOfMonth(4)
.withMonthOfYear(7)
.withYear(1776)
.println('EEE, 'dd/MM/yyyy HH:mm:ss')