Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
The script to calculate time difference in Android
String tStart = "09:27:37";
String tFinish = "15:24:04";
String[] sTimeHourMinSec = tStart.split(":");
int sHour = Integer.valueOf(sTimeHourMinSec[0]);
int sMin = Integer.valueOf(sTimeHourMinSec[1]);
int sSec = Integer.valueOf(sTimeHourMinSec[2]);
String[] fTimeHourMinSec = tFinish.split(":");
int fHour = Integer.valueOf(fTimeHourMinSec[0]);
int fMin = Integer.valueOf(fTimeHourMinSec[1]);
int fSec = Integer.valueOf(fTimeHourMinSec[2]);
int diffTotSec = (fHour - sHour) * 3600 + (fMin - sMin) * 60 + (fSec - sSec);
int diffHours = diffTotSec / 3600;
int diffMins = (diffTotSec % 3600) / 60;
int diffSecs = (diffTotSec % 3600) % 60;
System.out.println("Difference: " + diffHours + " h " + diffMins + " m " + diffSecs + " sec");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.