on 11-11-2015 12:44 PM
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.
Ramesh,
Refer the below link to get an idea
http://scn.sap.com/thread/3450107
If your struck will help you out.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
User | Count |
---|---|
84 | |
10 | |
10 | |
10 | |
7 | |
6 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.