cancel
Showing results for 
Search instead for 
Did you mean: 

UDF for calculating date minus 1 day

Former Member
0 Kudos

Hi All,

In my message mapping i have a field CPESPD (effective date) , and in my output i want this date(CPESPD) minus 1 day .

for eg : if CPESPD is 20090521 , then output should be 20090520,

so i wrote a UDF for this :

import java.util.Date;

import java.text.SimpleDateFormat;java.text.ParsePosition;

SimpleDateFormat sdf = new SimpleDateFormat("yyyymmdd");

Date date = sdf.parse(a[0],new ParsePosition(0));

Calendar cal = Calendar.getInstance();

cal.setTime(date);

cal.add(Calendar.DATE, -1);

date = cal.getTime();

String output = sdf.format(date);

result.addValue(output);

this UDF is working fine , but in cases where date is 20090201 , ideally out put should be 20090131

but this UDf gives output as 20080131 , it changes the year also along with day , but doesnt change the month.

Can anybody correct the UDf that i have written ?

Regards ,

Loveena.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi rajesh ,

I tried using Gregorian Calendar instaed of calendar , but still it doesnt work

Regards,

Loveena.

Former Member
0 Kudos

Got it use format yyyyMMdd instead of yyyymmdd

Rajesh

Answers (2)

Answers (2)

Former Member
0 Kudos

Thanks rajesh ,

That solved my problem!!!!!!!!!!!!

Former Member
0 Kudos

try using

cal.add(Calendar.DAY_OF_MONTH,-1);

if the above variable is not there then use GregorainCalendar instead of calendar

Rajesh

Edited by: Rajesh on May 22, 2009 1:13 PM