I have 2 hours to add up and store in a Long attribute called duration, so I do:
String source1 = "05:15:30";
String[] tokens1 = source1.split(":");
int secondsToMs1 = Integer.parseInt(tokens1[2]) * 1000;
int minutesToMs1 = Integer.parseInt(tokens1[1]) * 60000;
int hoursToMs1 = Integer.parseInt(tokens1[0]) * 3600000;
int total1 = secondsToMs1 + minutesToMs1 + hoursToMs1;
Then I add another hour:
String source12 = "03:05:10";
String[] tokens12 = source1.split(":");
int secondsToMs12 = Integer.parseInt(tokens12[2]) * 1000;
int minutesToMs12 = Integer.parseInt(tokens12[1]) * 60000;
int hoursToMs12 = Integer.parseInt(tokens12[0]) * 3600000;
int total12 = secondsToMs12 + minutesToMs12 + hoursToMs12;
and calculate the sum of both:
Long t = new Long(total1) + new Long(total12);
Now if I show the total again in hh:mm:ss format, the sum of both shows me:
String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(t),
TimeUnit.MILLISECONDS.toMinutes(t) % TimeUnit.HOURS.toMinutes(1),
TimeUnit.MILLISECONDS.toSeconds(t) % TimeUnit.MINUTES.toSeconds(1));
System.out.print(hms);
10:31:00
but it would have to show me a total of:
08:20:40
I think copypaste has played a trick on you:
should be: