我想计算餐厅的营业时间。我有两个字符串,例如:
String start_hour = "09:00";
String end_hour = "18:00";
例如当前时间:
Calendar now = Calendar.getInstance();
String current_hour = now.get(Calendar.HOUR_OF_DAY) + ":" + now.get(Calendar.MINUTE);
I calculate open hours with this method:
public boolean isRestaurantOpenNow() {
try {
Calendar now = Calendar.getInstance();
String current_hour = now.get(Calendar.HOUR_OF_DAY) + ":" + now.get(Calendar.MINUTE);
String st_hour = "09:00";
String en_hour = "18:00";
@SuppressLint("SimpleDateFormat") final SimpleDateFormat format = new SimpleDateFormat("HH:mm");
Date sth = null;
sth = format.parse(st_hour);
Date enh = format.parse(en_hour);
Date nowh = format.parse(current_hour );
if (nowh != null) {
if (nowh.before(enh) && nowh.after(sth)) {
// restaurant is open
return true;
} else {
// restaurant is close
return false;
}
}
} catch (ParseException ignored) {
}
return false;
}
但我有一些问题。当 start_hour 为 "13:00" 和 end_hour "05:00" 时,此方法工作错误。因为从第二天 05:00 开始。我怎么解决这个问题?