Custom Pega Function - Difference between Dates In Month

 Hi,

In this blog we will see now to create our own custom function for finding difference between dates, will give difference in the form of number of months.



Before Creating our function we need to create Library (Data Instance Rule). 

Go to Records --> Technical --> Library  (Right click create)

Library Name = ArtLib

RuleSet Name = Art


In Packages Tab, inside Java packages to Include


1) java.io.*;


2) java.util.*;


3) javax.xml.transform.*;


4) javax.xml.transform.stream.*;


5) java.time.*;


6) java.time.format.*;


7) java.time.temporal.*;


{checked} Library ready to compiled?


Now,

Go to Records --> Technical --> Function --> Right Click Create

Function Name = DifferencebetweenDatesInMonth 


In Input Parameters

StartDate      String (Java Type)

EndDate        String (Java Type)


In Output Parameters

Java data type = int

Pega type = Number


In Java Tab, write below Java Code,

Java Source


LocalDate local_date_1;

LocalDate local_date_2;

DateTimeFormatter   formatter_1 = DateTimeFormatter.ofPattern("yyyyMMdd",Locale.US)

String str_date_1 = StartDate;

String str_date_2 = EndDate;

local_date_1 = LocalDate.parse(str_date_1,formatter_1);

local_date_2 = LocalDate.parse(str_date_2,formatter_1);


Long months = ChronoUnit.MONTHS.between(local_date_1,local_date_2);

int m = Math.toIntExact(months);

return m;


To test above created function, Open Data Transform or Activity and use expression builder


   DifferencebetweenDatesInMonth("20201201","20231201")


Output = 36


Comments

Popular posts from this blog

pxSubscript and pxListSubscript Properties In Pega

Frequently Used Date Expressions In Pega

pyForEachCount In Pega