cancel
Showing results for 
Search instead for 
Did you mean: 

UDF for comparing Date

sherin_jose4
Participant
0 Kudos

Hi,

Currently we are working on a mapping logic where we have to compare two dates and the output should provide the number of days.

Say if the two inputs are 2012-02-26 and 2012-02-22, the output should be "4".

Could someone share me the exact UDF code for achieving this functionality where i need to consider the leap year as well.

Thanks,

Sherin Jose

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

with joda time libary(http://joda-time.sourceforge.net/) you can use:

import java.util.Date;
import org.joda.time.DateTime;
import org.joda.time.Days;

Date past = new Date(110, 5, 20); // June 20th, 2010
Date today = new Date(110, 6, 24); // July 24th
int days = Days.daysBetween(new DateTime(past), new DateTime(today)).getDays(); //

regards,

Robin

anupam_ghosh2
Active Contributor
0 Kudos

Hi Robin,

                 I think we need to install jar files to work with joda time as mentioned here http://joda-time.sourceforge.net/installation.html .  Could you please explain that in case I need to use joda time in my UDF (and not java mapping) instead of standard classes, how should I incorporate the jar files in the message mapping which contains the UDF? Looking forward to your able guidance in this regard.

Regards

Anupam

Former Member
0 Kudos

Hi,

you can upload jar files as "imported archive" like you do with XSLT and Java Mappings.

than you can import the java classes in your UDF like you do it with standard java classes.

(in the example screenshots i did that with the apache HTTP Client to be able to create lookups on a website)

regards,

Robin

anupam_ghosh2
Active Contributor
0 Kudos

Hi Robin,

             That's what I expected. Thank you for your kind response.

Finally it is suggested that lot of care has to be taken before using open source software or jar files.

The source has to be  verified, business has to give permission for its usage etc.

Thus would my opinion is to use standard classes as far as possible.

Hope yu would agree with me on this point.

Regards

Anupam 

Former Member
0 Kudos

Hi Anupam,

you are right ... of course business has to give permisson.

And a lof of care you should always take .. while building any kind of interface

But for Open Source i see a change of thinking in Business over the last years.

Even SAP starts more and more using Open Source for there own products.

Like SAPUI5 deeply integrate jQuery... and there are many more examples.

but we starting to become off-topic here

regards,

Robin

anupam_ghosh2
Active Contributor
0 Kudos

Hi Sherin,

               Please try this UDF

public static String compareDates(String startDate,String EndDate)

{

      String difference="";

try

{

       String pattern = "yyyy-MM-dd";

       long  diff;

       final long  MilliSecondsInADay=1000 * 60 * 60 * 24 ;

       java.text.SimpleDateFormat Dateformat = new java.text.SimpleDateFormat(pattern);

       java.util.Date endDate= Dateformat.parse(EndDate);

       java.util.Date StartDate= Dateformat.parse(startDate);

       diff=(endDate.getTime()-StartDate.getTime())/MilliSecondsInADay;

       if(diff<0)

       {

             diff=-diff;

       }

       difference=""+diff;

}

catch(java.text.ParseException e)

{

       e.printStackTrace();

}

return difference;

}

Regards

Anupam