Calculating date - 1

Hi all,

Given a date in yyyymmdd format how can I subtract one day (in Flow) from the date. Of course, I need to be concerned with leap years and such.

Thanks,
Harry

hi Harry,

Do you have to use flow? There is a PSUtilities java service called “date:incrementDate” that does exactly what you’re looking for. Just use -1 as the value for addDays

make sure your format is yyyyMMdd

And the usual suggestion when using a PSUtilities service is to copy it into a seperate package of your own.

-Cort

1 Like

Cort,

Thanks for the reply - I didn’t even know about PSUtilities. That just goes to show how much of a newbie that I am. Anyway, incrementDate is working just fine.

Thanks!

Hi ,

Could you please send me the jave code to increment date. I too have a requirement to increment date…and I do not have PSUtilities package

regards,
rds

Hi rds,

Try this code.

try {
IDataCursor pipelineCursor = pipeline.getCursor();
String    noOfDays = IDataUtil.getString( pipelineCursor, "noOfDays" );
String    pattern = IDataUtil.getString( pipelineCursor, "pattern" );
String    changeDate = IDataUtil.getString( pipelineCursor, "changeDate" );
String fromPattern = IDataUtil.getString( pipelineCursor, "fromPattern" );
pipelineCursor.destroy();

SimpleDateFormat sdf = new SimpleDateFormat(fromPattern);
Date from = sdf.parse(changeDate);
Calendar cal = Calendar.getInstance();
cal.setTime(from);
cal.add(Calendar.DATE,(new Integer(noOfDays)).intValue());
Date dt = new Date(cal.getTimeInMillis());

// input
IData input = IDataFactory.create();
IDataCursor inputCursor = input.getCursor();
Object date = new Object();
IDataUtil.put( inputCursor, "date", dt);
IDataUtil.put( inputCursor, "pattern", pattern );
inputCursor.destroy();
 
// output
IData     output = IDataFactory.create();
try{
    output = Service.doInvoke( "pub.date", "formatDate", input );
}catch( Exception e){}
IDataCursor outputCursor = output.getCursor();
    String    value = IDataUtil.getString( outputCursor, "value" );
outputCursor.destroy();

IDataCursor pipelineCursor_1 = pipeline.getCursor();
IDataUtil.put( pipelineCursor_1, "modifiedDate", value );
pipelineCursor_1.destroy();
}
catch(Exception e) {
 throw new ServiceException("Error occurred " + e);
}

Cheers,

KK

You can download the latest version of the PSUtilities package from the Advantage KnowledgeBase->Samples->Integration Server area.

Or click here after logging into Advantage.

1 Like

Hi,

Thanks a lot.
I downloaded the psutilities package!!

regards,
rds

Folks,

We can even write a small flow service to increment or decrement dates.

The logic goes as follow:

  1. Convert the date to YYYYMMDD format

  2. Start a repeat loop for the number of days to be added or subtracted

  3. Add/Subtract 1 based on increment/decrement

  4. format date and it will correct any invalid dates - for ex: 20061032 will be changed to 20061101 automatically. In fact it works for leap years tooo…

  5. Finally convert the date to the required format.

Hope this helps.
Bhavani Shankar.

I would not recommend this approach as a reliable method of doing date arithmetic.

This is a brute force approach that may work and even seem to work most of the time, but it is obviously inefficient and algorithmically unsound. It kind of accidentally works.

1 Like

I agree that this isn’t the best approach, but it should always work. The description for java.util.Date states that it will calculate the proper date even when the days are out of range for a given month, as Bhavani described. From the javadoc for java.util.Date:

Using pub.date:dateBuild and inputting:
pattern=yyyy/MM/dd
year=2006
month=1
day=325

Returns 2006/11/21, which is indeed the 325th day of the year. Other values, including large values such as 700 or 15000, return valid results as well (2007/12/01 and 2047/01/25, respectively).

Again, I agree that isn’t really a great approach (certainly a loop isn’t required) but it does (unaccidentally) work. :slight_smile:

P.S. “Out of range” months will work too.

1 Like

I stand corrected, but also in agreement with you that this solution is not recommended (especially the loop).

Mark

Thanks to this thread :slight_smile:
I am about to write java service to decrements date -1 to my flow service which is passing current date but this thread resolve my issue.

Is this the best flow service to decrements date -1 ?
Will it work in all test scenario’s even like leap year as well, just curious to know can’t take set back in production env
Or I need to move on with java service code logic to decrements Date -1

PSUtilities java service called “date:incrementDate”