cancel
Showing results for 
Search instead for 
Did you mean: 

Duration between two dates in SAP PI

Former Member
0 Kudos

Can anyone please help me to get duration between Given date and current date.

Example : If given date is 2012-03-29 then duration should be 5 Years and 3 months.

If given date is 2012-07-25 then duration should be 4 Years and 11months. I want to send these dates dynamically.

Thanks & Regards

Swetha

Accepted Solutions (0)

Answers (1)

Answers (1)

Muniyappan
Active Contributor
0 Kudos

can you please let us know your PI version? If it is on 7.5, then it should be easier to achieve it.

Please check below stackoverflow thread

https://stackoverflow.com/a/1116138/4286953

https://stackoverflow.com/a/27738766/4286953

if you run this on java 8, you will get the same results.

                LocalDate start = LocalDate.of(2012, 7, 25); // 
		LocalDate end = LocalDate.of(2017, 7, 04); //
		long years = ChronoUnit.YEARS.between(start, end);
		long months = ChronoUnit.MONTHS.between(start, end);
		System.out.println("years    " + years);
		System.out.println("months    " + months % 12);

Regars,

Muni

Former Member
0 Kudos

Hello Muni,

Thanks for your answer.

Can you please let me know how can i pass given date into this LocalDate?

Thanks

Swetha

Muniyappan
Active Contributor
0 Kudos

check the java api

https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html

below two methods will do the job for you.

static LocalDateparse(CharSequence text)

static LocalDatenow()

                String given_Date = "2012-07-25"; // pass the input to the UDF as string
		LocalDate start = LocalDate.parse(given_Date); // to create LocalDate Obj
		LocalDate end = LocalDate.now(); // get current date
		System.out.println(end.getYear()+"  "+end.getMonthValue()+"  "+end.getDayOfMonth());
		System.out.println(end.toString());

Results.

2017  7  5
2017-07-05

Similarly you can check the start date values also.