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: