cancel
Showing results for 
Search instead for 
Did you mean: 

Date subtraction in SAP PI

Former Member
0 Kudos

Hi Experts,

I have requirement regarding Date subtraction in SAP PI.

I have acondition in which few fields are coming from a file with data. Among, the different fields there is a start date field.

The condition is such that I have to transfer that part of the file in which the start date = currentdate-5;

Which means for the maybe 6 fields maybe wherever there is condition startdate = currentdate-5, that part of the file needs to be picked.

Now, I am unable to write the UDF in which this date calculation is there, also problem occurring when month is the previous one.

Please advice.

Accepted Solutions (1)

Accepted Solutions (1)

former_member472138
Active Contributor
0 Kudos

Date today = new Date();

*Check the date with your argument.

Calendar calendar = Calendar.getInstance();

calendar.add(Calendar.DATE, -5);

Regards

Pothana

Edited by: Pothana Yadav on Aug 3, 2011 8:47 AM

Answers (3)

Answers (3)

Former Member
0 Kudos

Check the below mentioned blog for your requirement..

/people/sarvesh.singh3/blog/2009/02/13/yes-it-is-possible-with-datetrans-function

anupam_ghosh2
Active Contributor
0 Kudos

Hi Aniruddha,

You have not specified the format in which you are receiving the date. For example mm/dd/yyyy or dd/mm/yyyy.

I have assumed dd/MM/yyyy. The format of date, you can alter after going through this link http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html

The UDF required is shown below


public String compareDates(String startDate,Container container){
String forwardDate="0";
		try
		{
			String pattern = "MM/dd/yyyy";
			int i;
			final int MilliSecondsInADay=1000 * 60 * 60 * 24 ;
		    java.text.SimpleDateFormat Dateformat = new java.text.SimpleDateFormat(pattern);
		    java.util.Date today=new java.util.Date();
		    java.util.Date fiveDayPreviousDate=new java.util.Date(Dateformat.format(new java.util.Date(today.getTime()- 5* MilliSecondsInADay)));
		    i=(Dateformat.parse(startDate)).compareTo(fiveDayPreviousDate);
		    forwardDate=new Integer(i).toString();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return forwardDate;
}

you can alter the "String pattern" as per your input date. This function returns "0", if the startDate is five days previous to current system date. These values will only be forwarded to target message structure. Ensure that the target field has minimum occurence as zero. Now you perform the following graphical mapping to achieve desired result

Link:[mapping|http://postimage.org/image/15k6bbuec/]

Here ITEMSET is the field carrying date. Here is the result

Link[ result screenshot|http://postimage.org/image/2zoe7em84/]

considering todays date is 08/03/2011 then the date 5 days earlier is 07/29/2011. So if the startDate value is 07/29/2011 then value is passed to target, for any other startdate value will not reach the target. Check this screenshot for positive output

Link[ result after mapping|http://postimage.org/image/xzt589tw/]

Hope this helps.

regards

Anupam

former_member854360
Active Contributor
0 Kudos

Use standard function datetrans and currentdate to achive your requirement with little tricks.......

like separately calculate month and date and year

or you can write UDF