Thursday, April 10, 2008

Intralink Scripting: Working with Dates in Locate

When recording dates during a Locate using the Scripting Options dialog, Intralink records it as a reference to the number of milliseconds since the beginning of 1970.

IL.addFilter( "Created On", ">=", new Object[]{new java.util.Date(1104555600000L)} ); 
It works fine, but with milliseconds it's a little hard to change to a specific date/time. It's still workable if your dates are relative to now, for example, models that have changed in the last 24 hours.

Date onedayago = new Date( (new Date()).getTime() - 24*3600*1000 );
IL.addFilter( "Created On", ">=", new Object[]{onedayago} );
Creating a new Date object sets it to the date/time right now and the getTime() method returns the number of milliseconds since 1970 for that object. For our "24 hours ago" Date object, we just need to subtract 24*3600*1000 milliseconds from that value and use it to create our object.

This is just quick and dirty calculations, if you need more precise calculations, you'll need to turn to Calendar based classes.

If we need to specify absolute date/time values, we'll have to incorporate the GregorianCalendar class into the mix. This class is the "official" way to specify absolute dates, provided you follow the Gregorian calendar.

The first step is to create the two objects and assign date/time values. Here were are setting year, month, day, hour, minute, and second values, but there are other options, such as add(), roll(), and setTime():
GregorianCalendar cal1 = new GregorianCalendar();
GregorianCalendar cal2 = new GregorianCalendar();
cal1.set(2008,0,1,0,0,0); // 2008-01-01 00:00:00
cal2.set(2008,2,15,23,59,0); // 2008-03-15 23:59:00

For the addFilter() method, we'll use the getTime() method on each GregorianCalendar object, which returns a Date object:
IL.addFilter( "Created On", ">=", new Object[]{ cal1.getTime() }
IL.addFilter( "Created On", "<=", new Object[]{ cal2.getTime() } ););

No comments: