cancel
Showing results for 
Search instead for 
Did you mean: 

Adding two dates in PI

Former Member
0 Kudos

Hi SDNers,

I have a requirement of adding two dates.

I have two input fields which contains date as input and mapped to a single output field.

The requirment for me is to add the two dates (from input) and pass to the output.

Can anybody help me getting this logic through UDF.

Thanks in Advance.

Regards

Jayaram.G

Accepted Solutions (0)

Answers (2)

Answers (2)

rajasekhar_reddy14
Active Contributor
0 Kudos

Hi try this code,

int date1 = 20091023;// first argumnet in UDF

int date2 = 11;//Second Argumnet in UDF

Date date = null;

String date1String = "" + date1;

DateFormat df = new SimpleDateFormat("yyyyMMdd");

try {

date = df.parse(date1String);

} catch (ParseException pe) {

pe.printStackTrace();

}

GregorianCalendar cal = new GregorianCalendar();

cal.setTime(date);

cal.add(Calendar.DAY_OF_YEAR, date2);

String result = cal.getTime().toString();

return result;

I tested this code in eclipse working fine.

may be some small modifications required..

cheers,

Raj

Former Member
0 Kudos

HI jaya,

Can you plz elaborate ur requirement.

I am not understanding what do u mean by two dates in a Input field and how do u want to handle in the output field.

Do u want to concatenate the dates in one single field or what?

Regards,

Rahul

Former Member
0 Kudos

Hi Rahul,

Thanks for your quick response.

Sorry the requirement that I had given previously is a little bit confusing.

My requirment is, in my input I have two fields, first field contains date(yyyymmdd) and second field contains days.

I have to add the second field days to the first field date and populate output.

Example: In my first field the input date coming in yyyymmdd format is 20090920 and in the second field the input is 15 (days).

The output that needs to be populated is 20091005.

Regards,

Jayaram.G

Former Member
0 Kudos

You can do this using Java Calender in your UDF...

Find code snippet at

http://snippets.dzone.com/posts/show/3966

http://www.rgagnon.com/javadetails/java-0101.html

-Siva Maranani

rajasekhar_reddy14
Active Contributor
0 Kudos

Hi,

Try this new code,previous code what i have given its give in string format,below code will give integer format.

import below packages in UDF

import java.text.DateFormat;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import java.util.GregorianCalendar;

int date1 = 20091023;//FIRST ARGUMENT

int date2 = 11;/SECOND ARGUMENT

Date date = null;

final String date1String = "" + date1;

final DateFormat df = new SimpleDateFormat("yyyyMMdd");

try {

date = df.parse(date1String);

} catch (ParseException pe) {

pe.printStackTrace();

}

final GregorianCalendar cal = new GregorianCalendar();

cal.setTime(date);

cal.add(Calendar.DAY_OF_YEAR, date2);

final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");

final String strDate = dateFormat.format(cal.getTime());

int resultDate = new Integer(strDate).intValue();

return resultDate;

it works fine..

Regards,

Raj