Sidebar

How to add/subtract minutes or hours etc. to a date?

0 votes
1.0K views
asked May 15, 2014 by rich-c-2789 (16,180 points)
edited Oct 23, 2018 by brandon-w-8204
I need to send the appointment end time.  I only have the appointment start time and the duration in minutes.  How can I add the minutes to the start time so that the hours roll correctly?

1 Answer

0 votes

// First import DateUtils. This class has a few apis that make it easy to add a value to a date.
var DateUtils = org.apache.commons.lang.time.DateUtils;

// Extract the start date from the message
var appointmentStartTime = qie.deduceDate(source.getNode("SCH-11.4"));

// Extract the duration from the message and convert it to an int. Times one will only work if this is just a number.
var durationInMinutes = source.getNode("SCH-9")*1;

// Use DateUtils to create a new date with the minutes added. To subtract add a negative number instead of a positve number
var appointmentEndTime = DateUtils.addMinutes(appointmentStartTime, durationInMinutes);

// Put the end time in the message
message.setNode("SCH-11.5", qie.formatDate('yyyyMMddHHmmss', appointmentEndTime));


For more ways to use DateUtils see:
http://commons.apache.org/proper/commons-lang/javadocs/api-2.3/index.html?org/apache/commons/lang/time/DateUtils.html

answered May 15, 2014 by rich-c-2789 (16,180 points)
edited Oct 23, 2018 by brandon-w-8204
...