How to Add or Subtract Hours from a Date using Groovy

Document created by sheng_liao462475 Employee on Dec 6, 2016Last modified by chris_stevens on Jan 19, 2017
Version 8Show Document
  • View in full screen mode

Use Case

Adding or subtracting time from a date value may be needed to "convert" a persisted last run date, such as changing PST time zone to EST time zone.

 

IMPORTANT

  • If using this approach to convert time zones, be advised you must know the two time zones and the time difference beforehand. Depending on the specific time zones, the actual time difference between them may vary throughout the year.
  • This approach will not modify the system last run date or system last successful run date.

 

See also Add or subtract 1 day (or more) to or from a Date using Groovy

 

Approach

  1. Use a dynamic process property "lsrun" to store the input date time value.
  2. Use a data process shape or custom scripting map function to do the time zones conversion. See the script below.
  3. Assign the dynamic process property to the target field.

 

Script

import java.util.Properties; 
import java.io.InputStream;
import com.boomi.execution.ExecutionUtil;
import java.util.Calendar;
import java.text.SimpleDateFormat;

// Retrieve Dynamic Process Property value
String lsrun= ExecutionUtil.getDynamicProcessProperty("lsrun");

// Parse date and add time
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd HHmmss.SSS");
Calendar calendar = new GregorianCalendar();
Date dateToFormat = format.parse(lsrun);
calendar.setTime(dateToFormat);
calendar.add(Calendar.HOUR, 3);
String result= calendar.getTime();

// Set Dynamic Process Property
ExecutionUtil.setDynamicProcessProperty("lsrun", result, false);

// Loop through documents (default script, do not modify)
for( int i = 0; i < dataContext.getDataCount(); i++ ) {
      InputStream is = dataContext.getStream(i);
      Properties props = dataContext.getProperties(i);
      dataContext.storeStream(is, props);
}
6 people found this helpful

Attachments

    Outcomes