Calculating Task Reminder, Escalation or Deadline days

Process Manager IconI recently had to set some reminders based on an actual time of the day (e.g. Midnight tonight and 12pm again tomorrow). This sounded easy until you realise that the Task Date object (the object used by Reminder, Escalation and Deadline) is not a Calendar or Date object, but a number of Days, Hours and Minutes until the Reminder was due. Awesome.

Below is an adaptation of a nice date calculation script from TechnoJeeves that allows you to calculate the number of days, hours and minutes between a certain date and time and transpose that into a Task Reminder object that Livecycle Process Manager can understand. You can also feed in a particular date string from a process variable and parse that if you want.

My example shows how to calculate the Hours and Minutes until midnight tonight (or tomorrow to be exact). There will never be any days involved as midnight is always less than 24 hours away.

Execute that script

To do this, you will need to create a Task Reminder variable called taskReminder.

This is the BeanShell script from my executeScript activity:

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

/**
 * Calculate the absolute difference between two Date without
 * regard for time offsets
 *
 * @param d1 Date one
 * @param d2 Date two
 * @return The fields day, hour, minute, second and millisecond
 */
long[] getTimeDifference(Date d1, Date d2) {
        long[] result = new long[5];
        Calendar cal = Calendar.getInstance();
        cal.setTimeZone(TimeZone.getTimeZone("UTC"));
        cal.setTime(d1);

        long t1 = cal.getTimeInMillis();
        cal.setTime(d2);

        long diff = Math.abs(cal.getTimeInMillis() - t1);
        final int ONE_DAY = 1000 * 60 * 60 * 24;
        final int ONE_HOUR = ONE_DAY / 24;
        final int ONE_MINUTE = ONE_HOUR / 60;
        final int ONE_SECOND = ONE_MINUTE / 60;

        long d = diff / ONE_DAY;
        diff %= ONE_DAY;

        long h = diff / ONE_HOUR;
        diff %= ONE_HOUR;

        long m = diff / ONE_MINUTE;
        diff %= ONE_MINUTE;

        long s = diff / ONE_SECOND;
        long ms = diff % ONE_SECOND;
        result[0] = d;
        result[1] = h;
        result[2] = m;
        result[3] = s;
        result[4] = ms;

        return result;
}

// today 
Date now = new Date();
Calendar date = new GregorianCalendar();
// reset hour, minutes, seconds and millis for midnight tonight
date.set(Calendar.HOUR_OF_DAY, 0);
date.set(Calendar.MINUTE, 0);
date.set(Calendar.SECOND, 0);
date.set(Calendar.MILLISECOND, 0);
// next day midnight is tonight midnight
date.add(Calendar.DAY_OF_MONTH, 1);

//caclulate the absolute difference      
long[] diff = getTimeDifference(date.getTime(), now);

//get hours and minutes for the reminder
patExecContext.setProcessDataIntValue( "/process_data/taskReminder/object/reminder/@hours", (int)diff[1] );
patExecContext.setProcessDataIntValue( "/process_data/taskReminder/object/reminder/@minutes", (int)diff[2] );

//enable the reminder
patExecContext.setProcessDataBooleanValue( "/process_data/taskReminder/object/@selected", true );

Now just assign the taskReminder variable to the Task Reminder in the Assign Task activity and your Reminder will be sent at midnight tonight!

aemblog

Everything AEM aka CQ5 based on my experience listed here.

Adobe AEM The Right Way

Best practices, tips, and tricks for your Adobe AEM project

/home/bkondepudi

A WCM journey with Day/Adobe CQ

Technoracle (a.k.a. "Duane's World")

A multi-purpose toolkit for the Adobe LiveCycle and AEM Forms developer.

Adobe LiveCycle Blog

A multi-purpose toolkit for the Adobe LiveCycle and AEM Forms developer.

A multi-purpose toolkit for the Adobe LiveCycle and AEM Forms developer.

XFA@Mobile

A multi-purpose toolkit for the Adobe LiveCycle and AEM Forms developer.

Code Monkey

Ramblings of a Developer