Skip to Content
avatar image
Former Member

Duration between two dates in SAP PI

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

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • Jul 04, 2017 at 11:34 AM

    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

    Add comment
    10|10000 characters needed characters exceeded

    • 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.