Skip to Content
0

Duration between two dates in SAP PI

Jul 04, 2017 at 09:54 AM

44

avatar image

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

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

Muni M Jul 04, 2017 at 11:34 AM
0

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

Show 2 Share
10 |10000 characters needed characters left characters exceeded

Hello Muni,

Thanks for your answer.

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

Thanks

Swetha

0

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.

0