Java Reference
In-Depth Information
Listing B.3. Using dates in a range with Java's Calendar class
For all its gifts, even Groovy can't tame Java's awkward java.util.Date and
java.util .Calendar classes, but it can make the code for using them a bit simpler.
Calendar is an abstract class with the factory method getInstance , so in Groovy I
call it by accessing the instance property. The Groovy JDK adds the format method
to Date , so it isn't necessary to separately instantiate SimpleDateFormat .
In the listing, after setting the year, month, and day, the Date instance is retrieved by in-
voking getTime . [ 8 ] In this case, that's equivalent to accessing the time property. The
dates are used as the boundaries of a range by the each method, which appends each one
to a list.
8 Yes, you read that correctly. You get the date by calling ... getTime . Hey, I didn't write it.
In fact, any class can be made into a range if it includes three features:
• A next() method, for forward iteration
• A previous() method, for backward iteration
• An implementation of the java.util.Comparable interface, for ordering
Here the range is used as the basis of a loop, where the dates are appended to a list.
 
Search WWH ::




Custom Search