Using date and time values in Gosu

Gosu date handling is simpler than Java provides in the java.util.Date class. Gosu and Guidewire enhancements also simplify coding in comparison to the DateUtil class. For example, the following code using the DateUtil class can be simplified:

var minDaysNeeded = 2
var nonRenewalNotificationLeadTime = 10
var newDate = DateUtil.addDays( DateUtil.currentDate(), minDaysNeeded + nonRenewalNotificationLeadTime )
print(newDate)

Compare the usage of the DateUtil.addDays method usage above to its equivalent in the following example:

var minDaysNeeded = 2
var nonRenewalNotificationLeadTime = 10
var newDate = Date.CurrentDate.addDays( minDaysNeeded + nonRenewalNotificationLeadTime )
print(newDate)

In both cases the output is:

Sat Aug 24 17:43:16 PDT 2019

The Date class defines many useful methods and static properties, such as Date.Today and Date.Yesterday.

Gosu enables you to iterate over a range of dates, which is often useful when testing date-sensitive code. The Date.to enhancement method returns a DateRangeIterator, as shown in the following example:

var start = Date.CurrentDate
var end = Date.CurrentDate.addDays(10)
for (d in start.to( end )) {
  print(d)
}

The output is:

Mon Aug 12 17:44:21 PDT 2019
Tue Aug 13 17:44:21 PDT 2019
Wed Aug 14 17:44:21 PDT 2019
Thu Aug 15 17:44:21 PDT 2019
Fri Aug 16 17:44:21 PDT 2019
Sat Aug 17 17:44:21 PDT 2019
Sun Aug 18 17:44:21 PDT 2019
Mon Aug 19 17:44:21 PDT 2019
Tue Aug 20 17:44:21 PDT 2019
Wed Aug 21 17:44:21 PDT 2019
Thu Aug 22 17:44:21 PDT 2019

The default iteration step, used in the example above, is one day. To change the step increment, call one of the DateRangeIterator.by methods, such as byMonth:

var start : Date = Date.CurrentDate
var end : Date = Date.CurrentDate.addDays(200)
for (d in start.to( end ).byMonth()) { //To step by month
  print(d)
}

The output is:

Mon Aug 12 17:45:18 PDT 2019
Thu Sep 12 17:45:18 PDT 2019
Sat Oct 12 17:45:18 PDT 2019
Tue Nov 12 17:45:18 PST 2019
Thu Dec 12 17:45:18 PST 2019
Sun Jan 12 17:45:18 PST 2020
Wed Feb 12 17:45:18 PST 2020
Note: The DateRangeIterator works in both directions. When starting with a later date, the iterator iterates backwards by the specified step value.