cancel
Showing results for 
Search instead for 
Did you mean: 

Calculation of date

Former Member
0 Kudos

Dear All,

I am having two variables in which one is having value as "20090415" and other is having value as "5". I need to get the output by subtracting the two variables as "20090410". Also, we need to take care if month is getting changed eg "20090501" - "2" = "20090429".

Please help me in creating UDF for the same.

Regards,

NJ

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Jain,

Create a value user defined function SubtractDays and take two input values BaseDate and Days.

Import: java.text.*;java.util.Date;java.util.Calendar;java.util.GregorianCalendar;

Add this code:

//write your code here

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

Date d = null;

try {d = sdf.parse(BaseDate);} catch (ParseException e) {}

GregorianCalendar gc = new GregorianCalendar();

gc.setTime(d);

gc.add(Calendar.DATE, Integer.parseInt(Days));

Date end = gc.getTime();

return sdf.format(end);

The first input for this udf should be your date. For the second input you need to send the no. of days, but here you concatenate with '-' so that if you send the input as 2 it goes as -2 as an second input to the udf. Then it should work for you.

Regards,

---Satish

Answers (1)

Answers (1)

MichalKrawczyk
Active Contributor
0 Kudos

hi,

there are soo many pages which show how to do that:

http://www.java-tips.org/java-se-tips/java.util/add-substract-day-month-year-to-a-date.html

http://www.kodejava.org/examples/24.html

just have a look at them and you're done with this issue

Regards,

Michal Krawczyk