How calculate difference between 23 to 1 (am) programmatically ?
How can I calculate the difference between two times that are in 24 hour format android?
2.2k views Asked by Sågär Śåxëńá At
3
There are 3 answers
0
On
How says @Bidhan A, the next time searches a little is a typical question... Then if you want to calculate a datetime or difference between two dates, as first step you need to convert date in milliseconds. The theory is 1 s is 1000 ms, 1 m is 60*1000 ms, 1 hour is 60*60*1000 ms, etc... The following code calculates the difference in days,hours,minutes or seconds between actual date and a posterior date and return string in this case as result:
public String TimeDifferenceCalculating(String EarlierDate) {
//Declaration of variables
String Result = "";
Date cDate = new Date();
String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(cDate);
String[] array1 = date.split(" ");
String[] array_YMD1 = array1[0].split("-");
String[] array_HMS1 = array1[1].split(":");
int ActualDate_Years = Integer.parseInt(array_YMD1[0]);
int ActualDate_Months = Integer.parseInt(array_YMD1[1]);
int ActualDate_Days = Integer.parseInt(array_YMD1[2]);
int ActualDate_Hours = Integer.parseInt(array_HMS1[0]);
int ActualDate_Minutes = Integer.parseInt(array_HMS1[1]);
int ActualDate_Seconds = Integer.parseInt(array_HMS1[2]);
String[] array2 = EarlierDate.split(" ");
String[] array_YMD2 = array2[0].split("-");
String[] array_HMS2 = array2[1].split(":");
int EarlierDate_Years = Integer.parseInt(array_YMD2[0]);
int EarlierDate_Months = Integer.parseInt(array_YMD2[1]);
int EarlierDate_Days = Integer.parseInt(array_YMD2[2]);
int EarlierDate_Hours = Integer.parseInt(array_HMS2[0]);
int EarlierDate_Minutes = Integer.parseInt(array_HMS2[1]);
int EarlierDate_Seconds = Integer.parseInt(array_HMS2[2]);
Calendar calendar1 = new GregorianCalendar(ActualDate_Years, ActualDate_Months-1, ActualDate_Days,ActualDate_Hours,
ActualDate_Minutes,ActualDate_Seconds);
long ActualDate_MiliSeconds = calendar1.getTimeInMillis();
Calendar calendar2 = new GregorianCalendar(EarlierDate_Years, EarlierDate_Months-1, EarlierDate_Days,EarlierDate_Hours,
EarlierDate_Minutes,EarlierDate_Seconds);
long EarlierDate_MiliSeconds = calendar2.getTimeInMillis();
long diff = ActualDate_MiliSeconds - EarlierDate_MiliSeconds;
if(diff >= 0) {
if (diff < (24 * 60 * 60 * 1000)) {
if (diff < (60 * 60 * 1000)) {
if (diff < (60 * 1000)) {
if (diff == 1000) {
Result = "1 second";
} else {
long diffSeconds = diff / (1000);
Result = String.valueOf(diffSeconds) + " seconds";
}
} else {
if (diff < (120 * 1000)) {
Result = "1 minute";
} else {
long diffMinutes = diff / (60 * 1000);
Result = String.valueOf(diffMinutes) + " minutes";
}
}
} else {
if (diff < (120 * 60 * 1000)) {
Result = "1 hour";
} else {
long diffHours = diff / (60 * 60 * 1000);
Result = String.valueOf(diffHours) + " hours";
}
}
} else {
if (diff < (48 * 60 * 60 * 1000)) {
Result = "1 day";
} else {
long diffDays = diff / (24 * 60 * 60 * 1000);
Result = String.valueOf(diffDays) + " days";
}
}
}
else{
Result = "";
}
return Result;
}
Tell me if I helped you and good programming!
You could do something like this: