Professional Documents
Culture Documents
This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process.
Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many
iterations to get reader feedback, pivot until you have the right book and build traction once you do.
2012 - 2013 Hubert A. Klein Ikkink (mrhaki)
Contents
Dates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Working with Dates . . . . . . . . . . . . . . . . . . . . . . . . . .
Setting Date and Calendar Values with Subscript Operators . . . . .
Convert Date to java.sql.Timestamp . . . . . . . . . . . . . . . . . .
Convert Date to Calendar . . . . . . . . . . . . . . . . . . . . . . .
Format Dates with TimeZone . . . . . . . . . . . . . . . . . . . . .
Parse Date.toString() Value . . . . . . . . . . . . . . . . . . . . . . .
Use the set Method to Define a Date or Calendar Value . . . . . . .
Create New Date from Old Date with Updates . . . . . . . . . . . .
Create New Date or Calendar from Existing and Set Property Value
Clear Time Portion of a Date . . . . . . . . . . . . . . . . . . . . . .
Date and Time Durations and the TimeCategory . . . . . . . . . . .
Loop Through Date and Calendar Ranges . . . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
1
1
2
2
3
3
4
5
5
6
6
7
8
Maps . . . . . . . . . . . . . . . . . . . . . .
Check if Maps are Equal . . . . . . . . . .
Sorting a Map . . . . . . . . . . . . . . . .
Turn a List into a Map . . . . . . . . . . .
Complex Keys in Maps . . . . . . . . . . .
Use inject Method on a Map . . . . . . . .
Intersect Maps . . . . . . . . . . . . . . . .
Subtracting Map Entries . . . . . . . . . .
Process Map Entries in Reverse . . . . . .
Getting a Submap from a Map . . . . . . .
Grouping Map Elements . . . . . . . . . .
Get Value from Map or a Default Value . .
Map with Default Values . . . . . . . . . .
Determine Min and Max Entries in a Map
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
10
10
10
11
11
12
12
12
13
13
14
15
16
16
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Dates
Working with Dates
Thanks to Groovys extensions to the JDK Date classes we can work with dates more easily. For example
we can now add and subtract days with the plus() and minus() methods. And because this methods are
mapped to the operators + and - we can write dense code.
Also the next() and previous() methods are implemented so we can use the ++ and -- operators to get
to a next or previous day. We can even use the subscript operator ([]) to get date fields, because of the
getAt() method Groovy adds to the Date class.
The Date class also contains parse() and format() methods to convert string to a date and to format a
date into a string.
// Date.parse() to convert String to Date.
date = new Date().parse('yyyy/MM/dd', '1973/07/09')
// We can use [] or getAt() to get date fields.
assert 1973 == date[Calendar.YEAR]
assert 6 == date[Calendar.MONTH]
assert 9 == date.getAt(Calendar.DATE)
dateNext = date.clone()
datePrevious = date.clone()
// We can use the + and - operators to add or
// subtract days.
nextDay = date + 1 // Or date.plus(1)
previousDay = date - 1 // Or date.minus(1)
// ++ operator to move one day ahead.
dateNext++ // Or dateNext.next()
assert dateNext == nextDay
// -- operator to move one day back.
datePrevious-- // Or datePrevious.previous()
assert datePrevious == previousDay
otherDate = new Date().parse('yyyy/MM/dd', '1973/07/21')
// Dates can be used in ranges.
assert 12 == (otherDate..<date).size()
// Set Locale to assert date formatting.
Locale.setDefault(Locale.US)
Dates
Dates
calendar[YEAR] == 2010
calendar[MONTH] == Calendar.DECEMBER
calendar[DATE] == 16
calendar.format('dd-MM-yyyy') == '16-12-2010'
calendar in Calendar
Dates
Dates
'12:00:00' == date.format('HH:mm:ss')
2010 == date[YEAR]
JUNE == date[MONTH]
1 == date.getAt(DATE)
Dates
Create New Date or Calendar from Existing and Set Property Value
Since Groovy 2.2 we can create a new Date or Calendar object from an existing Date and Calendar
object and directly change property values. We must use the copyWith() method and we pass a map
with property names and values as an argument. The newly created object will have the old values for
properties from the original object and the properties set in the map are overridden.
The following code shows the new copyWith() method:
import static java.util.Calendar.NOVEMBER
// Create original date.
def date = new Date().clearTime()
date.set(year: 2013, month: NOVEMBER, date: 18)
// Use copyWith to get new Date and
// immmediatelly set year to 2014.
def yearLater = date.copyWith(year: 2014)
assert yearLater.format('dd-MM-yyyy') == '18-11-2014'
Dates
Dates
Dates
Thursday
Wednesday
Tuesday
Monday
In the next sample we use the upto() method on the Calendar class:
// upto() also works on Calendar objects.
def to = Calendar.instance
to.set(year: 2013, month: Calendar.NOVEMBER, date: 18)
def from = Calendar.instance
from.set(year: 2013, month: Calendar.NOVEMBER, date: 13)
from.upto(to) {
if (it[Calendar.DATE] % 2 == 0) {
print 'Even'
} else {
print 'Odd'
}
println ' date'
}
Maps
Check if Maps are Equal
With Groovy 1.8 the equals() method is added to Map. This means we can check if maps are equals. They
are equals if both maps have the same size, and keys and values are the same.
def map1 = [user: 'mrhaki', likes: 'Groovy', age: 37]
def map2 = [age: 37.0, likes: 'Groovy', user: 'mrhaki']
def map3 = [user: 'Hubert Klein Ikkink', likes: 'Groovy']
assert
assert
assert
assert
map1.equals(map2)
map1 == map2
!map1.equals(map3)
map2 != map3
Sorting a Map
Maps dont have an order for the elements, but we may want to sort the entries in the map. Since Groovy
1.7.2 we can use the sort() method which uses the natural ordering of the keys to sort the entries. Or we
can pass a Comparator to the sort() method to define our own sorting algorithm for the keys.
def m = [sort: 'asc', name: 'test', paginate: true, max: 100]
def expectedKeys = ['max', 'name', 'paginate', 'sort']
// Since 1.7.2
assert expectedKeys == m.sort()*.key
// Since 1.7.2
assert expectedKeys == m.sort( { k1, k2 -> k1 <=> k2 } as Comparator )*.key
// Sorting before Groovy 1.7.2
assert expectedKeys == new TreeMap(m)*.key
// Sort by closure.
assert expectedKeys == m.sort { e1, e2 -> e1.key <=> e2.key }*.key
11
Maps
def m = [
(new Date(109, 11, 1)): 'date key',
(-42): 'negative number key',
(false): 'boolean key',
(key): 'variable key'
]
m.(true) = 'boolean key' // Key is converted to String.
m.(2 + 2) = 'number key'
m[(key + 1)] = 'number key' // Key keeps to be Integer.
assert
assert
assert
assert
assert
// Key
assert
assert
assert
Maps
12
Intersect Maps
Since Groovy 1.7.4 we can intersect two maps and get a resulting map with only the entries found in both
maps.
def m1 = [a: 'Groovy', b: 'rocks', c: '!']
def m2 = [a: 'Groovy', b: 'rocks', c: '?', d: 'Yes!']
assert [a: 'Groovy', b: 'rocks'] == m1.intersect(m2)
assert [1: 1.0, 2: 2] == [1: 1.0, 2: 2].intersect([1: 1, 2: 2.0])
Maps
13
Maps
14
15
Maps
// Key
assert
assert
assert
assert
assert
assert
assert
// Use groupBy.
def group = m.groupBy(groupIt)
assert 2 == group.size()
assert group.params
assert group.sort
// Key for Map with key/value pairs.
assert 'desc' == group.sort.sort
assert 2 == group.params.size()
assert 'Groovy' == group.params.q1
assert 'q1' == group.params.keySet().toArray()[0]
assert 'Grails' == group.params.q2
assert group.params instanceof Map
assert group.params.q1 instanceof String
16
Maps
'one' == m.start
42 == m['1']
'Groovy rocks!' == m['I say']
3 == m.size()
Maps
17