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:
Found an issue in documentation? Write to us.
Â