1

如何确定当前时间是否介于其他两个时间对象之间?

例如:当前:15:23,开始时间:22:00,结束时间:06:00。所以当前时间不在这两个其他时间之间,而是这个“23:47”。

*我不关心日期只关心时间

我试着这样做:

Time startTime = getStartTime();
Time endTime = getEndTime();

Calendar c = Calendar.getInstance();
Time now = new Time(c.getTimeInMillis());

if (now.before(endTime) && now.after(startTime)) {
    return true;
}

return false;

但它并不能很好地工作。他们不是任何错误,但它只是不起作用。

更准确地说,问题是 15:35.after(22:00) 返回 true... 我不知道为什么。我认为问题出在日期上,但我不知道是什么

4

4 回答 4

0

试试这个代码

long startTime = getStartTime(); // return the as long
long endTime = getEndTime();// return the value as long

Calendar c = Calendar.getInstance();
long now = c.getTimeInMillis();

if (now>startTime && now<endTime) {

    return true;

}

return false;
于 2012-12-28T13:08:30.993 回答
0

我想你只是有一个错字。您想使用 (now.before(endTime) && now.after(startTime))。

像这样:

Time startTime = getStartTime();
Time endTime = getEndTime();

Calendar c = Calendar.getInstance();
Time now = new Time(c.getTimeInMillis());

if (now.before(endTime) && now.after(startTime)) {
    return true;
}

return false;
于 2012-12-28T13:16:44.103 回答
0

我找到了一个解决方案:

    Time startTime = getStartTime();
    Time endTime = getEndTime();

    Calendar startCalendarTime = Calendar.getInstance();
    startCalendarTime.set(Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH, startTime.getHours(), startTime.getMinutes(), 0);

    Calendar endCalendarTime = Calendar.getInstance();
    endCalendarTime.set(Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH, endTime.getHours(), endTime.getMinutes(), 0);

    Calendar midnightTime = Calendar.getInstance();
    midnightTime.set(Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH, 23, 59, 0);

    Calendar nowCalendarTime = Calendar.getInstance();
    nowCalendarTime.set(Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH);

    if (startCalendarTime.after(endCalendarTime)) {

        if (nowCalendarTime.after(midnightTime)) {
            startCalendarTime.add(Calendar.DAY_OF_YEAR, -1);
        } else {
            endCalendarTime.add(Calendar.DAY_OF_YEAR, 1);
        }

    }
    int hours = nowCalendarTime.getTime().getHours();
    int minutes = nowCalendarTime.getTime().getMinutes();
    nowCalendarTime.set(Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH, hours, minutes, 0);

    if (nowCalendarTime.after(startCalendarTime) && nowCalendarTime.before(endCalendarTime)) {
        return true;
    }

    return false;
于 2012-12-28T17:42:14.500 回答
0

当我的应用程序不会宣布语音内容时,我需要“安静时间”。

这是我为便于阅读而格式化的解决方案:

private boolean quietTimesOk(final Context ctx) {

    final Calendar cal = Calendar.getInstance();
    final int hour = cal.get(Calendar.HOUR_OF_DAY);
    final int minute = cal.get(Calendar.MINUTE);
    final int combined = (hour * 60) + minute;

    final int qtsHour = getQuietTimeStartHour(ctx);
    final int qtsMinute = getQuietTimeStartMinute(ctx);
    final int qtsCombined = (qtsHour * 60) + qtsMinute;

    final int qteHour = getQuietTimeEndHour(ctx);
    final int qteMinute = getQuietTimeEndMinute(ctx);
    final int qteCombined = (qteHour * 60) + qteMinute;

    if (qtsCombined > qteCombined) {            
        Log.i("quietTimesOk", "- qtsCombined > qteCombined");

        if (combined < qtsCombined && combined > qteCombined) {
            Log.i("quietTimesOk", "- returning true");
            return true;
        }

    } else {            
        Log.i("quietTimesOk", "- qtsCombined < qteCombined");

        if (combined < qtsCombined || combined > qteCombined) {
            Log.i("quietTimesOk", "- returning true");
            return true;
        }
    }

    Log.i("quietTimesOk", "- returning false");

    return false;
}
于 2013-05-08T01:52:21.523 回答