/
How can I manipulate with datetimes?

How can I manipulate with datetimes?

Question

I am working with the following date format:

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

How can I subtract or add 5 minutes?

Answer

You can use the Calendar instance to add/subtract part of the date.

def cal = Calendar.getInstance() cal.add(Calendar.MINUTE, -5) def targetTimestamp = api.targetDate()?.format("yyyy-MM-dd") + 'T' + (cal).format("HH:mm:ss")

Or another option is:

def dt = new DateTime(api.targetDate()) return dt.plusMinutes(5)

Note that api.targetDate() gets you the time midnight (basically the day part only).

The DateTime object is of type org.joda.time.DateTime.

Related content

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
Get Datamart Time Dimension from a Date
Get Datamart Time Dimension from a Date
More like this
How do I get today's date in a Groovy script?
How do I get today's date in a Groovy script?
More like this

Found an issue in documentation? Write to us.