How can I calculate the difference between two times that are in 24 hour format android?

2.2k views Asked by At

How calculate difference between 23 to 1 (am) programmatically ?

3

There are 3 answers

3
Gabriella Angelova On

You could do something like this:

String firstTime = "14:29:50";
String secondTime = "09:12:43";

//create dates from the strings (if you have  strings not dates)
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");  

Date date1 = null;
Date date2 = null;
try {
    date1 = format.parse(firstTime);
    date2 = format.parse(secondTime);
} catch (ParseException e) {
    e.printStackTrace();
}    

//get difference between the two times and then convert it in seconds, minutes or hours (what you need)
long diff = date1.getTime() - date2.getTime();
long diffSeconds = diff / 1000;         
long diffMinutes = diff / (60 * 1000);         
long diffHours = diff / (60 * 60 * 1000);                      
System.out.println("Time as difference in seconds: " + diffSeconds + " seconds.");         
System.out.println("Time as difference in minutes: " + diffMinutes + " minutes.");         
System.out.println("Time as difference in hours: " + diffHours + " hours."); 
0
Anshuman Borah On

You can do something like this:

Date date1, date2; // Insert value in date1 & date2 as your need

long dateDiff = date1.getTime() - date2.getTime();           
dateDiff = ((dateDiff / (1000 * 60 * 60)) + 1) / 24;
0
Merlí Escarpenter Pérez 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!