i have some problem while finding difference between times, if i try to find difference in todays time (say t1 = "08:00:00" and t2 = "10:00:00" then it is giving correct output,)but when i try to find the difference like t1 = "20:00:00"(which is todays time) and t2 ="08:00:00"(which is next day morning),i want the output as 12 hours but i a getting wrong outputs. kindly help me.
String t1 = "20:00:00";
String t2 = "12:00:00";
String t3 = "24:00:00";
SimpleDateFormat sDf = new SimpleDateFormat("HH:mm:ss");
Date d1 = sDf.parse(t1);
Date d2 = sDf.parse(t2);
Date d3 = sDf.parse(t3);
long s1,s2,s3,s4,dif1,dif2,dif3,minutes,hrs;
dif1 = d2.getTime() - d1.getTime();
s1 = dif1/1000;
System.out.println("s1 "+s1);
if(s1<0){
dif2 = d3.getTime() - d1.getTime();
s2 = dif2/1000;
System.out.println("s2 "+s2);
dif3 = d2.getTime();
s3 = dif3/1000;
System.out.println("s3 "+s3);
s4 = s2+s3;
minutes = s4 / 60;
s4 = s4 % 60;
hrs = minutes / 60;
minutes = minutes % 60;
System.out.println("time difference is : "+hrs+": "+minutes+" :"+s4);
}else{
minutes = s1 / 60;
s1 = s1 % 60;
hrs = minutes / 60;
minutes = minutes % 60;
System.out.println("time difference is : "+hrs+": "+minutes+" :"+s1);
}