How do I get today's date in a Groovy script?

Question

I found the method api.calendar() but it returns the date set for a quote. Only if the date is not set, it returns today.

Answer

Use new Date(), an object of java.util.Date.

To work with dates, create new Date() and put it to the constructor of GregorianCalendar, an object returned by api.calendar().

def calendar = api.calendar(new Date()) def calendarEnd = api.calendar(new Date()) calendarEnd.add(Calendar.YEAR,1) calendarEnd.set(Calendar.DAY_OF_MONTH, calendarEnd.getActualMaximum(Calendar.DAY_OF_MONTH));

Then you can get the Date() this way:

sDate = api.parseDate("YYYY-MM-dd",startValidity) eDate = api.parseDate("YYYY-MM-dd",endValidity)

Depending on the format, another alternative is:

def targetTimestamp = api.targetDate()?.format("yyyy-MM-dd") + 'T' + (new Date()).format("HH:mm:ss") return targetTimestamp

This returns the date with hours, minutes and seconds.

 

It is important to note that the Filter API requires a String as parameters when using dates for filtering.

An example:

def c = Calendar.getInstance() c.setTime(api.targetDate()) c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH)) def dateTo = c.getTime() c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH)) def dateFrom = c.getTime() api.logInfo("TARGET DATE !!!!!!!!!!!", api.targetDate()) api.logInfo("DATE FROM !!!!!!!!!!!", dateFrom?.format("dd/MM/YYYY")) api.logInfo("DATE TO!!!!!!!!!!!", dateTo?.format("dd/MM/YYYY")) api.global.ippRateData = api.findLookupTableValues("IPPRate", Filter.greaterOrEqual("key1", dateFrom?.format("dd/MM/YYYY")), Filter.lessOrEqual("key2", dateTo?.format("dd/MM/YYYY")), Filter.equal("key4", api.global.pItemStyle), Filter.equal("attribute5", api.global.pProcurementGroup))?.getAt(0)

Found an issue in documentation? Write to us.