...
| Code Block |
|---|
import groovy.time.* //reuse Extras category from a previous example... use( [Extras, org.codehaus.groovy.runtimetime.TimeCategory] ){ assert 10.years.class == DatumDependentDuration assert 10.years.toString() == new DatumDependentDuration( 10, 0, 0, 0, 0, 0, 0 ).toString() assert 4.months.toString() == new DatumDependentDuration( 0, 4, 0, 0, 0, 0, 0 ).toString() assert 7.weeks.toString() == new Duration( 49, 0, 0, 0, 0 ).toString() assert 5.days.toString() == new Duration( 5, 0, 0, 0, 0 ).toString() assert 12.hours.toString() == new TimeDuration( 12, 0, 0, 0 ).toString() assert 15.minutes.toString() == new TimeDuration( 0, 15, 0, 0).toString() assert 13.seconds.toString() == new TimeDuration( 0, 0, 13, 0 ).toString() assert 750.milliseconds.toString() == new TimeDuration( 0, 0, 0, 750 ).toString() assert 1.day.toString() == new Duration( 1, 0, 0, 0, 0 ).toString() //we can use the singular name for any of these... assert 25.minute.toString() == new TimeDuration( 0, 25, 0, 0 ).toString() //...even when not grammatical in English } |
...
| Code Block |
|---|
import groovy.time.* //reuse Extras category from a previous example... use( [Extras, org.codehaus.groovy.runtimetime.TimeCategory] ){ assert (10.years + 4.months).class == DatumDependentDuration assert (10.years + 4.months).toString() == new DatumDependentDuration( 10, 4, 0, 0, 0, 0, 0 ).toString() assert (10.years.plus(4.months) ).toString() == (10.years + 4.months).toString() //alternative method name assert (4.months + 10.years).toString() == (10.years + 4.months).toString() //all duration operations are commutative assert (10.years + 4.weeks).class == DatumDependentDuration assert (5.days + 7.weeks).class == Duration assert (5.days + 17.hours).class == TimeDuration assert (10.minutes + 5.seconds).class == TimeDuration //adding a DatumDependentDuration and a TimeDuration gives a //specially-defined TimeDatumDependentDuration... assert (10.years + 12.hours).toString() == new TimeDatumDependentDuration( 10, 0, 0, 12, 0, 0, 0 ).toString() assert (10.years + 12.hours).class == TimeDatumDependentDuration assert ( 10.years + new TimeDatumDependentDuration( 0, 0, 0, 12, 0, 0, 0 ) ).class == TimeDatumDependentDuration assert ( 10.days + new TimeDatumDependentDuration( 0, 0, 0, 12, 0, 0, 0 ) ).class == TimeDatumDependentDuration assert ( 10.minutes + new TimeDatumDependentDuration( 0, 0, 0, 12, 0, 0, 0 ) ).class == TimeDatumDependentDuration assert ( new TimeDatumDependentDuration( 0, 0, 0, 12, 0, 0, 0 ) + new TimeDatumDependentDuration( 0, 0, 0, 0, 10, 0, 0 ) ).class == TimeDatumDependentDuration //subtracting durations... assert (10.years - 4.months).class == DatumDependentDuration assert (10.years - 4.months).toString() == new DatumDependentDuration( 10, -4, 0, 0, 0, 0, 0 ).toString() assert (10.years.minus(4.months) ).toString() == (10.years - 4.months).toString() //alternative method name assert (10.years - 12.hours).class == DatumDependentDuration assert (5.days - 7.weeks).class == Duration assert (5.days - 17.hours).class == TimeDuration assert (10.minutes - 5.seconds).class == TimeDuration assert (10.years - 4.weeks).class == DatumDependentDuration } |
...
| Code Block |
|---|
import groovy.time.* //reuse Extras category from a previous example... use( [Extras, org.codehaus.groovy.runtimetime.TimeCategory] ){ def today= new Date(), tomorrow= today + 1, dayAfter= today + 2, nextWeek= today + 7 //days-only Date arithmetic assert ( today + 7.days ).toString() == nextWeek.toString() //use Date and duration together assert ( today.plus(7.days) ).toString() == ( today + 7.days ).toString() //alternative method name assert ( 7.days + today ).toString() == nextWeek.toString() //commutative assert ( nextWeek - 6.days ).toString() == tomorrow.toString() assert ( nextWeek.minus(6.days) ).toString() == tomorrow.toString() //alternative method name assert ( nextWeek - dayAfter ).toString() == 5.days.toString() //subtract two dates to get a duration //some handy operations... [2.days.ago, 3.days.from.now, 3.days.from.today].each{ assert it.class == java.sql.Date } } |
...
| Code Block |
|---|
System.setProperty('user.timezone', 'GMT') //we can set the default time zone
def tz= new SimpleTimeZone( -8*(60*60*1000), 'Somewhere',
Calendar.MARCH, 1, 0, 2*(60*60*1000),
Calendar.OCTOBER, 31, 0, 2*(60*60*1000) )
def cal= new GregorianCalendar( tz )
//create a calendar with today's date in a specified time zone
cal= Calendar.getInstance( tz ) //another way
cal= new GregorianCalendar(2009, Calendar.JULY, 22)
//we can create a calendar with the default time zone...
cal.timeZone= tz //...then set the time zone
assert cal.timeZone == tz
assert cal.get(Calendar.ZONE_OFFSET) == -8*(60*60*1000)
assert cal.get(Calendar.DST_OFFSET) == (60*60*1000)
assert Calendar.FIELD_COUNT == 17
//the number of fields such as DAY_OF_YEAR and ZONE_OFFSET in Calendar
//we can test whether two time zones have the same rules...
assert tz.hasSameRules(
new SimpleTimeZone( -8*(60*60*1000), 'Somewhere Else',
Calendar.MARCH, 1, 0, 2*(60*60*1000),
Calendar.OCTOBER, 31, 0, 2*(60*60*1000)
) )
assert ! tz.hasSameRules(
new SimpleTimeZone( -8*(60*60*1000), 'Somewhere Else',
Calendar.APRIL, 1, 0, 2*(60*60*1000),
Calendar.OCTOBER, 31, 0, 2*(60*60*1000)
) )
//some methods available within TimeCategory...
use(org.codehaus.groovy.runtimetime.TimeCategory){
cal= new GregorianCalendar( tz )
def today= cal.time
println today.timeZone
println today.daylightSavingsOffset //returns a duration
def nextWeek= today + 7
println( (nextWeek - today).daylightSavingsOffset )
//a duration also has a daylight savings time offset
println( nextWeek.getRelativeDaylightSavingsOffset( today ) )
}
//we can test if a certain date is in daylight saving time for a time zone...
assert tz.inDaylightTime( new GregorianCalendar(1990, Calendar.MAY, 5).time )
assert ! tz.inDaylightTime(
new GregorianCalendar(1990, Calendar.NOVEMBER, 5).time )
//we can set the first year daylight savings time operates...
tz.startYear= 1973
assert ! tz.inDaylightTime( new GregorianCalendar(1971, Calendar.MAY, 5).time )
//some extra format codes for dates...
println String.format('%tZ', cal)
//to see a string representing the time zone, eg, GMT-07:00
println String.format('%tz', cal) //numeric offset from GMT, eg, -0800
assert String.format('%tc', cal) ==
String.format('%ta %<tb %<td %<tT %<tZ %<tY', cal)
//we can view the Gregorian changeover date...
assert String.format( '%ta %<td %<tb %<tY', cal.gregorianChange ) ==
'Fri 15 Oct 1582' //default for GMT time zone
cal= new GregorianCalendar()
cal.set(1582, Calendar.OCTOBER, 15)
cal.time
assert String.format( '%ta %<td %<tb %<tY', cal.time - 1 ) ==
'Thu 04 Oct 1582' //the day before the big change
//check for leap years (this instance method acts like a static method)...
[1999, 1998, 1997, 1900, 1800, 1700].each{ assert ! cal.isLeapYear(it) }
[2000, 1996, 1992, 1600, 1500, 1400].each{ assert cal.isLeapYear(it) }
//1500 and before use Julian calendar rules
|