Comparison of a date and time field to a range of values

In SQL, you use the BETWEEN keyword to apply comparison predicates to an inclusive range of column values in the WHERE clause. With the query builder APIs, you use the between method to apply a range predicate to entity fields. The following Gosu code applies the between method to values in the creation timestamp field of Address entity instances to select addresses that were created in the previous month.

uses gw.api.database.Query 
uses gw.api.database.DBFunction
uses gw.api.database.QuerySelectColumns
uses gw.api.path.Paths
uses gw.api.util.DateUtil

// Query the Company instances for a range of creation dates. 
var firstDayOfCurrentMonth = DateUtil.currentDate().FirstDayOfMonth
var previousMonthStart = DateUtil.addMonths(firstDayOfCurrentMonth, -1)
var previousMonthEnd = DateUtil.addDays(firstDayOfCurrentMonth, -1)

// Make a query of Address instances.
var query = Query.make(Address) 
// Query for addresses created in the previous month.
query.between(DBFunction.DateFromTimestamp(query.getColumnRef("CreateTime")), 
              previousMonthStart, previousMonthEnd)

// Order the result by creation date and iterate the items fetched.
for (address in query.select().orderBy(QuerySelectColumns.path(Paths.make(Address#CreateTime)))) {
  print(address.DisplayName + ": " + address.CreateTime)
}

See also