cancel
Showing results for 
Search instead for 
Did you mean: 

date difference in days in sap cpi

former_member229127
Participant
0 Kudos

How to acheive days different from source am getting endate and start date and difference i need to send to target.

eg : end date= 01.01.2006 - start date= 01.01.2005

target : 365

Regards,
Sandya

Accepted Solutions (1)

Accepted Solutions (1)

Sriprasadsbhat
Active Contributor
0 Kudos

Hello Sandya,

You can tweak the code a bit to get the same.

import com.sap.it.api.mapping.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.*
import java.lang.*;
import java.util.*;
def String customFunc1(String P1,String P2) {
         String inpStartDate =P1.toString();
         String inpEndDate = P2.toString();
         
         SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
         Date EmpStartDate = sdf.parse(inpStartDate);
         Date EmpEndDate = sdf.parse(inpEndDate); 


         //Use dateDiffInDays to get difference in days
         def dateDiffInDays=EmpEndDate - EmpStartDate
         dateDiffInDays=dateDiffInDays+1
         return dateDiffInDays.toString();
}

Regards,

Sriprasad Shivaram Bhat

Answers (4)

Answers (4)

Sriprasadsbhat
Active Contributor

Hello Sandhya,

Below code would do the above.Since you have not mentioned whether you are going to use it in Script step or as customFunction inside MessageMapping I am passing below code you can adjust it as per your need.

import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.*
import java.lang.*;
import java.util.*;


//PassYourinputDates for startDate and endDate from your script or custom function
ef startDate="01.01.2006"
def endDate="31.12.2006"
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
Date EmpStartDate = sdf.parse(startDate);
Date EmpEndDate = sdf.parse(endDate); 

//Use dateDiffInDays to get difference in days
def dateDiffInDays=EmpEndDate - EmpStartDate
//println dateDiffInDays

Regards,

Sriprasad Shivaram Bhat

former_member229127
Participant
0 Kudos

Hi Sriprasad,

Am receving the data from the source system which is dynamic which is in the format of dd-mm-yyyy so should i go a head and change the date with the date format and use in the message mapping?

Regards,
Sandya

Sriprasadsbhat
Active Contributor

Hello Sandhya,

If you want to go with message mapping below is the approach.

Create a custom function ( similar to UDF concept of SAP PI/PO ) and take 2 dates as input and get the output mapped to desired field.

Custom Function Script:

import com.sap.it.api.mapping.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.*
import java.lang.*;
import java.util.*;


def String customFunc1(String P1,String P2) {
         String inpStartDate =P1.toString();
         String inpEndDate = P2.toString();
         
         SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
         Date EmpStartDate = sdf.parse(inpStartDate);
         Date EmpEndDate = sdf.parse(inpEndDate); 


         //Use dateDiffInDays to get difference in days
         def dateDiffInDays=EmpEndDate - EmpStartDate
       
         return dateDiffInDays.toString();
}


Regards,

Sriprasad Shivaram Bhat

former_member229127
Participant
0 Kudos

Hi Sriprasad,

Thanks for the reply !!!

While am extracting the data in the source xsd am not getting the data.I have change the context but data is not coming the structure.Can i know why it is so.

The payload for the source side.

<?xml version="1.0" encoding="UTF-8"?> <root> <row> <FIELD5>TEST</FIELD5> <FIELD6>USER</FIELD6> <FIELD7>TEST</FIELD7> <FIELD8></FIELD8> <FIELD9>GCA</FIELD9> <FIELD10>DEFAULT</FIELD10> <FIELD11>C1</FIELD11> <FIELD12>CC</FIELD12> <FIELD13>CC1</FIELD13> <FIELD14></FIELD14> <FIELD15></FIELD15> <FIELD16></FIELD16> <FIELD17></FIELD17> <FIELD18></FIELD18> <FIELD19></FIELD19> <FIELD20></FIELD20> <FIELD21></FIELD21> <FIELD22></FIELD22> <FIELD23></FIELD23> <FIELD24></FIELD24> <FIELD25></FIELD25> <FIELD26></FIELD26> <FIELD27></FIELD27> <FIELD28></FIELD28> <FIELD29></FIELD29> <FIELD30></FIELD30> <FIELD31></FIELD31> <FIELD32></FIELD32> <FIELD33></FIELD33> <FIELD34></FIELD34> <FIELD35></FIELD35> <FIELD36></FIELD36> <FIELD37></FIELD37> <FIELD38></FIELD38> <FIELD39>337F</FIELD39> <FIELD40>87</FIELD40> <FIELD41>DEFAULT</FIELD41> <FIELD42>SAR</FIELD42> <FIELD43>INDIA</FIELD43> <FIELD44>2019-09-18</FIELD44> <FIELD45>2020-01-18</FIELD45> <FIELD46></FIELD46> <FIELD47>2020-01-18</FIELD47> <FIELD48></FIELD48> <FIELD49>CASH</FIELD49> <FIELD50>1700.00</FIELD50> <FIELD51>1700.00</FIELD51> <FIELD52>1700.00</FIELD52> <FIELD53>GC</FIELD53> <FIELD54>2019-09-18</FIELD54> <FIELD55>3</FIELD55> <FIELD56></FIELD56> <FIELD57></FIELD57> <FIELD58></FIELD58> <FIELD59></FIELD59> <FIELD60></FIELD60> <FIELD61>CONFERENCE</FIELD61> <FIELD62></FIELD62> <FIELD63></FIELD63> <FIELD64></FIELD64> <FIELD65></FIELD65> <FIELD66></FIELD66> <FIELD67></FIELD67> <FIELD68></FIELD68> <FIELD69></FIELD69> <FIELD70></FIELD70> <FIELD71></FIELD71> <FIELD72></FIELD72> <FIELD73></FIELD73> <FIELD74></FIELD74> <FIELD75></FIELD75> <FIELD76></FIELD76> <FIELD77>Q</FIELD77> <FIELD78></FIELD78> <FIELD79>AW</FIELD79> <FIELD80></FIELD80> <FIELD81></FIELD81> <FIELD82>0.00</FIELD82> <FIELD83>Y</FIELD83> <FIELD84></FIELD84> <FIELD85>TRAVEL</FIELD85> <FIELD86></FIELD86> <FIELD87></FIELD87> <FIELD88></FIELD88> <FIELD89>96</FIELD89> <FIELD90>ALLOWANCE</FIELD90> </row> </root>

Regards,

Sandya

r_herrmann
Active Contributor

Hi sandhya.ravi ,

I suggest to use a Groovy Mapping to solve this task. You could use something like that:

def dateEnd = new Date().parse("dd.MM.yyyy", "01.01.2006")
def dateStart = new Date().parse("dd.MM.yyyy", "01.01.2005")
def daysDifference = dateEnd.minus(dateStart)

Regards,
Raffael

Sriprasadsbhat
Active Contributor
0 Kudos

Hello Sandhya,

Its very difficult to debug the same just by looking at screenshot:(. Please do check you source data and then try to display queue for both the fields .If you are getting values for both the input values then try recreating the custom function ( dont copy paste whole code,just edit the content with wherever you want to put above code and try not to delete function definition line instead just add the new field ).

Regards,
Sriprasad Shivaram Bhat

former_member229127
Participant
0 Kudos

Hello Sri Prasad,

Thanks for the reply and i have sorted out the issue of the context, one final thing i need to add +1 for the out put can i go ahead with add function with the output as passing the value 1 as a constant in mapping function as we do in PI or should there be a change in Groovy.

Regards,
Sandya