计算两个日期选择器之间的差异秒数

时间:2015-02-28 04:24:41

标签: android datetime time

我试着按照这里的建议:

android calculating time difference for setting alarm

所以这就是我所做的:

    Calendar start = Calendar.getInstance();
    start.set(Calendar.HOUR_OF_DAY, timeSchedule.getCurrentHour());
    start.set(Calendar.MINUTE, timeSchedule.getCurrentMinute());

    Calendar end = Calendar.getInstance();
    end.set(Calendar.HOUR_OF_DAY, toTime.getCurrentHour());
    end.set(Calendar.MINUTE, toTime.getCurrentMinute());

    long startMillis = start.getTimeInMillis();
    long endMillis = end.getTimeInMillis();
    long diffSec = TimeUnit.MILLISECONDS.toSeconds(endMillis - startMillis);

toTimetimeScheduleTimePicker的位置。但是,结果不正确。我怀疑是因为AMPM

请帮助我,非常感谢你的帮助。

更新

感谢@Raghunandan,这是我目前的代码:

    Calendar start = Calendar.getInstance();
    start.set(Calendar.HOUR, timeSchedule.getCurrentHour());
    start.set(Calendar.MINUTE, timeSchedule.getCurrentMinute());

    if (start.get(Calendar.AM_PM) == Calendar.AM)
        start.set(Calendar.AM_PM, Calendar.AM);
    else
        start.set(Calendar.AM_PM, Calendar.PM);

    Calendar end = Calendar.getInstance();
    end.set(Calendar.HOUR, toTime.getCurrentHour());
    end.set(Calendar.MINUTE, toTime.getCurrentMinute());

    if (end.get(Calendar.AM_PM) == Calendar.AM)
        end.set(Calendar.AM_PM, Calendar.AM);
    else
        end.set(Calendar.AM_PM, Calendar.PM);

    long startMillis = start.getTimeInMillis();
    long endMillis = end.getTimeInMillis();
    long diffSec = TimeUnit.MILLISECONDS.toSeconds(endMillis - startMillis);

如果两个Timpickers都是AM或PM,结果都是真的。然而,如果例如用户将7PM设置为1AM,则结果将是假的(<0或负值)。我该怎么办?

1 个答案:

答案 0 :(得分:1)

尝试

Calendar start = Calendar.getInstance();
start.set(Calendar.HOUR, 6);
start.set(Calendar.MINUTE, 30);
start.set(Calendar.AM_PM,Calendar.AM);

Calendar end = Calendar.getInstance();
end.set(Calendar.HOUR, 6); // 12 hour format
end.set(Calendar.MINUTE, 30);
end.set(Calendar.AM_PM,Calendar.PM); // AM or PM

long startMillis = start.getTimeInMillis();
long endMillis = end.getTimeInMillis();
long diffSec = TimeUnit.MILLISECONDS.toSeconds(endMillis) - TimeUnit.MILLISECONDS.toSeconds(startMillis) ;
long diffHour = TimeUnit.MILLISECONDS.toHours(endMillis) - TimeUnit.MILLISECONDS.toHours(startMillis) // 12 hour diff

使用Calendar.Hour表示12小时格式,Calendar.HOUR_OF_DAY表示24小时格式。

http://developer.android.com/reference/java/util/Calendar.html#AM_PM

http://developer.android.com/reference/java/util/Calendar.html#HOUR

编辑:

第二天晚上7点到凌晨1点

Calendar start = Calendar.getInstance();
start.set(Calendar.HOUR, 7);
start.set(Calendar.MINUTE, 30);
start.set(Calendar.AM_PM,Calendar.PM);

Calendar end = Calendar.getInstance();
int day = (start.get(Calendar.DAY_OF_MONTH)+1);
end.set(Calendar.DAY_OF_MONTH,day);
end.set(Calendar.HOUR, 1);
end.set(Calendar.MINUTE, 30);
end.set(Calendar.AM_PM,Calendar.AM);

long startMillis = start.getTimeInMillis();
long endMillis = end.getTimeInMillis();
long diffSec = TimeUnit.MILLISECONDS.toSeconds(endMillis) - TimeUnit.MILLISECONDS.toSeconds(startMillis) ;
long diffHour = TimeUnit.MILLISECONDS.toHours(endMillis) - TimeUnit.MILLISECONDS.toHours(startMillis) ;

编辑2:

Calendar start = Calendar.getInstance();
start.set(Calendar.HOUR_OF_DAY, 21);
start.set(Calendar.MINUTE, 30);

if (start.get(Calendar.AM_PM) == Calendar.AM)
        start.set(Calendar.AM_PM, Calendar.AM);
else
        start.set(Calendar.AM_PM, Calendar.PM);

Calendar end = Calendar.getInstance();
end.set(Calendar.HOUR_OF_DAY, 4);
end.set(Calendar.MINUTE, 30);
int day = (start.get(Calendar.DAY_OF_MONTH)+1);
end.set(Calendar.DAY_OF_MONTH,day);

if (end.get(Calendar.AM_PM) == Calendar.AM)
        end.set(Calendar.AM_PM, Calendar.AM);
else
        end.set(Calendar.AM_PM, Calendar.PM);

long startMillis = start.getTimeInMillis();
long endMillis = end.getTimeInMillis();
long diffSec = TimeUnit.MILLISECONDS.toSeconds(endMillis - startMillis);
long diffHour = TimeUnit.MILLISECONDS.toHours(endMillis) - TimeUnit.MILLISECONDS.toHours(startMillis) ;

Toast.makeText(this,""+diffHour, Toast.LENGTH_LONG).show();

注意:您可能还需要一天选择器。