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)
, multiple selections available,
Related content
Why does api.getElement(DATE).format produce an unexpected result?
Why does api.getElement(DATE).format produce an unexpected result?
More like this
How to Get Date Time for Specific Timezone and Date in Specific Format
How to Get Date Time for Specific Timezone and Date in Specific Format
More like this
Can I use the Groovy library in Analytics?
Can I use the Groovy library in Analytics?
More like this
Date & Time
Date & Time
Read with this
How can I manipulate with datetimes?
How can I manipulate with datetimes?
More like this
Get Datamart Time Dimension from a Date
Get Datamart Time Dimension from a Date
More like this
Found an issue in documentation? Write to us.