【发布时间】:2019-10-06 02:03:26
【问题描述】:
如果时间在两个值之间,我有一个显示警报的功能。
例如:
开始 = 07:00:00
结束 = 17:00:00
assert start != null;
int from = Integer.valueOf(start.replace(":", ""));
assert end != null;
int to = Integer.valueOf(end.replace(":", ""));
Date date = new Date();
Calendar c = Calendar.getInstance();
c.setTime(date);
int t = c.get(Calendar.HOUR_OF_DAY) * 100 + c.get(Calendar.MINUTE);
boolean isBetween = to > from && t >= from && t <= to || to < from && (t >= from || t <= to);
a = isBetween ? 1 : 0;
if(a == 1) {
alert_f();
// a is never 1...
}
问题是a 永远不会是 1,即使实际时间介于开始和结束之间。
任何想法为什么?
【问题讨论】:
-
如果您的时间字符串还包括秒数,您在计算 t 时不应该也包括日历中的秒数吗?
-
@Luksprog 感谢您的回答!如何在我的函数中添加秒数?或从开始/结束变量中删除?你能帮帮我吗?
标签: android time android-calendar datetime-comparison