如何确定当前时间是否介于两个其他时间对象之间?
例如:当前: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。之后(22:00)返回真实......我不知道为什么。 我认为这个问题与日期不符,但我不知道是什么
答案 0 :(得分: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;
答案 1 :(得分:0)
我觉得你刚收到一个错字。你想使用(now.before(endTime)&amp;&amp; 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;
答案 2 :(得分: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;
答案 3 :(得分: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;
}