cancel
Showing results for 
Search instead for 
Did you mean: 

UDF for elapsed time

former_member190543
Participant
0 Kudos

Hi all,

Can you help me with the UDF for calculating the time difference please?

Inputs to the function will be 2 variables; t1 and t2 in the format HH:mm:ss

Required time difference (td) should be in the format of HH:mm:ss. 

For example:

t1 = 12:45:00  

t2 = 14:10:00

Required result: td = 01:25:00


Also, if t2 is smaller (earlier) than t1, then we need to add 24:00:00 to t2 and then subtract t1.

Many thanks in advance.

Ramesh.

Accepted Solutions (0)

Answers (1)

Answers (1)

former_member186851
Active Contributor
0 Kudos

Ramesh,

Refer the below link to get an idea

http://scn.sap.com/thread/3450107

If your struck will help you out.

former_member190543
Participant
0 Kudos

Hi Raghuraman,

Thanks for your link. I have implemented my requirement using graphical mapping to get it working temporarily, but it is very big and cluttered to look at.

I will try the code from the link and let you know of any issues.

Many thanks.

Ramesh.

anupam_ghosh2
Active Contributor
0 Kudos

Hi Ramesh,

                    Please can you try below code


public static String  timeDifference(String t1, String t2) throws StreamTransformationException

    {

        try{

            java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("HH:mm:ss");

            String result="";

            java.util.Date d1 = null;

            java.util.Date d2 = null;

          

            d1=format.parse(t1);

            d2=format.parse(t2);

          

            long diff = d2.getTime() - d1.getTime();

            if(diff<0)

            {

              

                java.util.GregorianCalendar cal = new java.util.GregorianCalendar();

                cal.setTime(d2);

                cal.add(Calendar.DATE,1);

                d2=cal.getTime();

                diff = d2.getTime() - d1.getTime();

             

            }

            long diffSeconds = diff / 1000 % 60;

            long diffMinutes = diff / (60 * 1000) % 60;

            long diffHours = diff / (60 * 60 * 1000) % 24;

          

          

          

          

            if(diffHours<10){

                result="0"+diffHours+":";

            }

            else

            {

                result=diffHours+":";

            }

            if(diffMinutes<10){

                result+="0"+diffMinutes+":";

            }

            else

            {

                result+=diffMinutes+":";

            }

            if(diffSeconds<10){

                result+="0"+diffSeconds;

            }

            else

            {

                result+=diffSeconds;

            }

          

            return result;

        }

        catch(Exception e)

        {

            e.printStackTrace();

            throw new StreamTransformationException(e.getMessage());

        }

      

}

output

------------

01:45:00 (t1),14:10:00 (t2)

difference=12:25:00

01:45:55 (t1),01:45:56 (t2)

difference=00:00:01

02:45:56 (t1),01:45:56 (t2)

difference=23:00:00

02:45:56 (t1),02:45:55 (t2)

difference=23:59:59

Regards

Anupam