Forum Discussion

Department_of_T's avatar
Department_of_T
Contributor
12 years ago

Add calendar months to date in groovy (groovy.time.TimeCateg

Hi,
How can i add 3 calendar months to a variable in my groovy script assertion?
If the original date is 30/4/14 and i add 3 months then the result is 30/7/14 and not 31/7/14. eg add 3 months to last day in april should give me the last day in august and not just change the month digit from 4 to 7.

import groovy.time.TimeCategory
def XMLDate = context.expand( '${JDBC Request to verify acc_account_items#ResponseAsXml#//Results[1]/ResultSet[1]/Row[1]/DEALING_TO_DATE[1]}' )
def SQLdate = context.expand('${JDBC Request to find vehicle#ResponseAsXml#//Results[1]/ResultSet[1]/Row[1]/LICENCE_EXPIRY_DATE[1]}' )
def shortSQLDate = new Date().parse("yyyy-MM-dd",SQLdate)
def shortXMLDate = new Date().parse("yyyy-MM-dd",XMLDate)
use(TimeCategory) {
shortSQLDatePlusOne=shortSQLDate + 3.month
}
log.info shortSQLDatePlusOne
log.info shortXMLDate
assert shortSQLDatePlusOne == shortXMLDate