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.

Found an issue in documentation? Write to us.