public class TimeDiff {
int seconds;
int minutes;
int hours;
public TimeDiff(int hours, int minutes, int seconds) {
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
}
public static TimeDiff timeDifference(TimeDiff start, TimeDiff end) {
TimeDiff timediff = new TimeDiff(0, 0, 0);
if (start.seconds > end.seconds) {
end.minutes--;
end.seconds += 60;
}
timediff.seconds = end.seconds - start.seconds;
if (start.minutes > end.minutes ) {
end.hours--;
end.minutes += 60;
}
timediff.minutes = end.minutes - start.minutes;
timediff.hours = end.hours - start.hours;
return timediff;
}
public static void main(String args[]) {
TimeDiff start = new TimeDiff(7, 11, 13);
TimeDiff end = new TimeDiff(10, 46, 27);
TimeDiff timediff = timeDifference(start, end);
System.out.printf("%d:%d:%d - ", start.hours, start.minutes, start.seconds);
System.out.printf("%d:%d:%d ", end.hours, end.minutes, end.seconds);
System.out.printf("= %d:%d:%d\n", timediff.hours, timediff.minutes, timediff.seconds);
}
}
/*
run:
7:11:13 - 10:46:27 = 3:35:14
*/