7

我正在开发和 android 应用程序,我需要计算两次之间的差异。我需要计算 24 小时的时间差异,以及两天时间之间的差异(例如今天下午 5 点到明天早上 9 点)。

我已经尝试了下面的代码,来计算仅适用于 24 小时的差异,

String dateStart = "08:00:00";
String dateStop = "13:00:00";

//HH converts hour in 24 hours format (0-23), day calculation
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");

Date d1 = null;
Date d2 = null;

try 
{
    d1 = format.parse(dateStart);
    d2 = format.parse(dateStop);

    //in milliseconds
    long diff = d2.getTime() - d1.getTime();
    long diffHours = diff / (60 * 60 * 1000) % 24;
    Log.e("test",diffHours + " hours, ");
}
catch (Exception e) 
{
    // TODO: handle exception
} 
4

6 回答 6

13

先生,您可以轻松使用 java 功能。long difference = date2.getTime() - date1.getTime(); 看看这个链接,这将对你有所帮助。

于 2013-09-20T04:27:44.623 回答
10

找到适当时差的正确方法:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
Date startDate = simpleDateFormat.parse("22:00:59");
Date endDate = simpleDateFormat.parse("23:00:10");

long difference = endDate.getTime() - startDate.getTime(); 
if(difference<0)
{
    Date dateMax = simpleDateFormat.parse("24:00:00");
    Date dateMin = simpleDateFormat.parse("00:00:00");
    difference=(dateMax.getTime() -startDate.getTime() )+(endDate.getTime()-dateMin.getTime());
}
int days = (int) (difference / (1000*60*60*24));  
int hours = (int) ((difference - (1000*60*60*24*days)) / (1000*60*60)); 
int min = (int) (difference - (1000*60*60*24*days) - (1000*60*60*hours)) / (1000*60);
int sec = (int) (difference - (1000*60*60*24*days) - (1000*60*60*hours) - (1000*60*min)) / (1000);
Log.i("log_tag","Hours: "+hours+", Mins: "+min+", Secs: "+sec); 

结果将是:小时:0,分钟:59,秒:11

于 2015-07-30T13:30:24.357 回答
7
于 2017-07-15T21:32:15.190 回答
2

如果您确定早上 9 点是第二天,您也可以尝试这样的事情,您可以添加一天并计算差异:

String string1 = "05:00:00 PM";
    Date time1 = new SimpleDateFormat("HH:mm:ss aa").parse(string1);
    Calendar calendar1 = Calendar.getInstance();
    calendar1.setTime(time1);

    String string2 = "09:00:00 AM";
    Date time2 = new SimpleDateFormat("HH:mm:ss aa").parse(string2);
    Calendar calendar2 = Calendar.getInstance();
    calendar2.setTime(time2);
    calendar2.add(Calendar.DATE, 1);

    Date x = calendar1.getTime();
    Date xy = calendar2.getTime();
    long diff = x.getTime() - xy.getTime();
    diffMinutes = diff / (60 * 1000);
    float diffHours = diffMinutes / 60;
    System.out.println("diff hours" + diffHours);
于 2013-09-20T05:47:50.433 回答
0
Try simple piece of code using For 24 hour time
StartTime = "10:00";
EndTime = "13:00";
here starthour=10 and end hour=13 

if(TextUtils.isEmpty(txtDate.getText().toString())||TextUtils.isEmpty(txtDate1.getText().toString())||TextUtils.isEmpty(txtTime.getText().toString())||TextUtils.isEmpty(txtTime1.getText().toString()))
    {
        Toast.makeText(getApplicationContext(), "Date/Time fields cannot be blank", Toast.LENGTH_SHORT).show();
    }
    else {
        if (starthour > endhour) {
            Toast.makeText(getApplicationContext(), "Start Time Should Be Less Than End Time", Toast.LENGTH_SHORT).show();
        } else if (starthour == endhour) {
            if (startmin > endmin) {
                Toast.makeText(getApplicationContext(), "Start Time Should Be Less Than End Time", Toast.LENGTH_SHORT).show();
            }
            else{
                tvalid = "True";
            }
        } else {
            // Toast.makeText(getApplicationContext(),"Sucess"+(endhour-starthour)+(endmin-startmin),Toast.LENGTH_SHORT).show();
            tvalid = "True";
        }
    }
same for date also
于 2017-07-14T06:19:03.690 回答
-2

I worked it out this way:

Date time1 = new SimpleDateFormat("HH:mm:ss aa").parse(string1);
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(time1);

Date time2 = new SimpleDateFormat("HH:mm:ss aa").parse(string2);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(time2);

if(calendar2.get(Calendar.AM_PM) == 1 && calendar1.get(Calendar.AM_PM) == 0)     {
     calendar2.add(Calendar.DATE, 1);
}
long diff = calendar1.getTimeInMillis() - calendar2.getTimeInMillis()

This will help to find the difference in time round the clock.

于 2015-04-20T11:14:59.873 回答