Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
def c1= new GregorianCalendar(1995, Calendar.SEPTEMBER, 5, 19, 35, 30, 750)

//dates...
assert String.format('%tY/%<tm/%<td', c1) == '1995/09/05'
assert String.format('%tA %<te %<tB %<ty', c1) == 'Tuesday 5 September 95'
assert String.format('century:%tC, month:%<tb, day:%<te', c1) ==
    'century:19, month:Sep, day:5'
assert String.format('month:%th, day of year:%<tj, day of week:%<ta', c1) ==
    'month:Sep, day of year:248, day of week:Tue' //'h' same as 'b'

//times...
assert String.format('%tH:%<tM:%<tS.%<tL', c1) == '19:35:30.750'
assert String.format('%tI%<tp, %<tl%<tp, nanoseconds:%<tN', c1) ==
    '07pm, 7pm, nanoseconds:750000000'
assert String.format('%ts', c1) == '810300930'
    //seconds since start of 1-Jan-1970 GMT
assert String.format('%tQ', c1) == '810300930750'
    //milliseconds since start of 1-Jan-1970 GMT
assert String.format('%tk',
            new GregorianCalendar(1995, Calendar.SEPTEMBER, 5, 6, 35)) == '6'

//shortcut formats...
assert String.format('%tF', c1) == '1995-09-05' //date as '%tm/%td/%ty'
assert String.format('%tD', c1) == '09/05/95' //date as '%tY-%tm-%td'
assert String.format('%tT', c1) == '19:35:30' //time as '%tH:%tM:%tS'
assert String.format('%tR', c1) == '19:35' //time as '%tH:%tM'
assert String.format('%tr', c1) == '07:35:30 PM' //time as '%tI:%tM:%tS %Tp'

//additionally...
assert String.format('%tF', new Date(0)) == '1970-01-01'
    //we can supply a Date instead of a Calendar
assert String.format('%tF', 0L) == '1970-01-01' //we can also supply a long

assert String.format('...%15tF...', 0L) == '...     1970-01-01...' //width 15
assert String.format('...%-15tF...', 0L) == '...1970-01-01     ...'
    // '-' flag to left-justify


From Groovy 1.5.7 / 1.6.x,  you may use Date.format() directly. Refer to GROOVY-3066 for details.

After setting fields, we must call any get(), add(), or roll() method, or access the 'timeInMillis' or 'time' properties, to cause other relevant fields to update themselves:

Code Block
System.setProperty('user.timezone', 'GMT')

def c= new GregorianCalendar()
c.set( Calendar.ERA, GregorianCalendar.AD )
c.set( Calendar.YEAR, 1949 )
c.set( Calendar.MONTH, Calendar.OCTOBER )
c.set( Calendar.DATE, 31 )
assert String.format('%tF %<ta', c) == '1949-10-31 Mon'

//properties for calculating WEEK_OF_YEAR and WEEK_OF_MONTH fields...
c.firstDayOfWeek = Calendar.SUNDAY //Sunday in most countries, Monday in others
c.minimalDaysInFirstWeek = 1

assert c.get(Calendar.ERA) == GregorianCalendar.AD &&
       c.get(Calendar.YEAR) == 1949 &&
       c.get(Calendar.MONTH) == 9 && //dates range from 0 to 11, so October
       c.get(Calendar.MONTH) == Calendar.OCTOBER && //alternatively
       c.get(Calendar.DAY_OF_MONTH) == 31 &&
       c.get(Calendar.WEEK_OF_YEAR) == 45 && //range from 1 to 53
       c.get(Calendar.WEEK_OF_MONTH) == 6 && //range from 1 to 6
       c.get(Calendar.DAY_OF_YEAR) == 304 &&
       c.get(Calendar.DAY_OF_WEEK) == 2 && //Monday
       c.get(Calendar.DAY_OF_WEEK_IN_MONTH) == 5

//changing the month uses the same year and day of month...
c.set(Calendar.MONTH, Calendar.AUGUST )
c.time //cause other fields to update themselves
assert String.format('%tF %<ta', c) == '1949-08-31 Wed'

c.set(Calendar.MONTH, Calendar.APRIL )
    //...but may cause adjustment to roll into following month
c.time
assert String.format('%tF %<ta', c) == '1949-05-01 Sun'

c.set(Calendar.DATE, 31 )
c.set(Calendar.MONTH, Calendar.FEBRUARY )
c.set(Calendar.MONTH, Calendar.SEPTEMBER )

//rolling into following month only occurs when other fields update themselves,
//call this method to trigger it...
c.time


assert String.format('%tF %<ta', c) == '1949-10-01 Sat'
    //...so Feb-28 DIDN'T roll into Mar-03

//changing the day of month uses the same month and year...
c.set( Calendar.DATE, 1 ); c.time
assert String.format('%tF %<ta', c) == '1949-10-01 Sat'

//changing the day of year adjusts the month, day, and other date fields...
c.set(Calendar.DAY_OF_YEAR, c.get(Calendar.DAY_OF_YEAR) + 2 ); c.time
assert String.format('%tF %<ta', c) == '1949-10-03 Mon'

//changing the week of year keeps the same day of week, but adjusts

//the other date fields...
c.set(Calendar.WEEK_OF_YEAR, c.get(Calendar.WEEK_OF_YEAR) + 3 ); c.time
assert String.format('%tF %<ta', c) == '1949-10-24 Mon'

//changing the week of month keeps both the same month and day of week...
c.set(Calendar.WEEK_OF_MONTH, c.get(Calendar.WEEK_OF_MONTH) - 2 ); c.time
assert String.format('%tF %<ta', c) == '1949-10-10 Mon'

//changing the day of week in month also keeps both the

//same month and day of week...
c.set(Calendar.DAY_OF_WEEK_IN_MONTH, c.get(Calendar.DAY_OF_WEEK_IN_MONTH) - 1 )
c.time
assert String.format('%tF %<ta', c) == '1949-10-03 Mon'

//changing the day of week keeps the same week in year...
c.set( Calendar.DAY_OF_WEEK, Calendar.SATURDAY ); c.time
assert String.format('%tF %<ta', c) == '1949-10-08 Sat'
c.set( Calendar.DAY_OF_WEEK, Calendar.SUNDAY ); c.time
assert String.format('%tF %<ta', c) == '1949-10-02 Sun'

...

Code Block
import groovy.time.*

class Extras{
  static toString(BaseDuration it){
    def list= []
    if(it.years != 0) list<< "$it.years yrs"
    if(it.months != 0) list<< "$it.months mths"
    if(it.days != 0) list<< "$it.days days"
    if(it.hours != 0) list<< "$it.hours hrs"
    if(it.minutes != 0) list<< "$it.minutes mins"
    if(it.seconds != 0 || it.millis != 0) list<< "$it.seconds.$it.millis secs"
    list.join(', ')
  }
}

//enable utility methods for duration classes using 'category' syntax,
//introduced in a later tutorial...
use(Extras){
  [ {new TimeDuration( 12, 30, 0, 0 )}:   '12 hrs, 30 mins',
    {new TimeDuration( 4, 12, 30, 0, 0 )}:'4 days, 12 hrs, 30 mins',
    {new Duration( 4, 12, 30, 0, 500 )}:  '4 days, 12 hrs, 30 mins, 0.500 secs',
    {new DatumDependentDuration( 7, 6, 0, 12, 30, 0, 0 )}:
                                          '7 yrs, 6 mths, 12 hrs, 30 mins',
  ].each{
    assert it.key().toString() == it.value
  }
}

def convertToMilliseconds= { yr, mth, day, hr, min, sec, mil->
  mil + 1000g*( sec + 60g*( min + 60g*( hr + 24g*(
     day + 30g*( mth + 12g*yr )
  ))))
}

assert new TimeDuration( 12, 30, 0, 0 ).toMilliseconds() ==
    convertToMilliseconds( 0, 0, 0, 12, 30, 0, 0 )
    //ignores 61-second leap minutes
assert new Duration( 114, 12, 30, 0, 0 ).toMilliseconds() ==
    convertToMilliseconds( 0, 0, 114, 12, 30, 0, 0 )
    //ignores 25-hour daylight-saving days
assert new DatumDependentDuration( 5, 1, 14, 12, 30, 0, 0 ).toMilliseconds() !=
    convertToMilliseconds( 5, 1, 14, 12, 30, 0, 0 )
    //considers 31-day months and leap-years

...

Code Block
import groovy.time.*

//reuse Extras category from a previous example...
use( [Extras, org.codehaus.groovy.runtime.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
}

...