Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Question

I am working with the following date format:

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

How can I subtract or add 5 minutes?

Cheers,

Jochen

...

hiddentrue

...

Answer

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

Code Block
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:

Code Block
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.