如何计算24小时格式android两次之间的差异?

时间:2015-06-12 06:31:35

标签: android difference

如何以编程方式计算23到1(am)之间的差异?

3 个答案:

答案 0 :(得分:2)

您可以这样做:

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

long dateDiff = date1.getTime() - date2.getTime();           
dateDiff = ((dateDiff / (1000 * 60 * 60)) + 1) / 24;

答案 1 :(得分:0)

你可以这样做:

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."); 

答案 2 :(得分:0)

@Bidhan A怎么说,下次搜索一点是一个典型的问题......那么如果你想计算一个日期时间或两个日期之间的差异,作为第一步,你需要以毫秒为单位转换日期。理论是1秒是1000毫秒,1米是60 * 1000毫秒,1小时是60 * 60 * 1000毫秒等...下面的代码计算实际日期和日期之间的天,小时,分钟或秒的差异 后验日期 ,在这种情况下返回字符串作为结果:

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;
}

告诉我,如果我帮助你并做好编程!