レストランの営業時間を計算したい。たとえば、次の 2 つの文字列があります。
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」の場合、このメソッドは正しく動作しません。翌日から5時だから。どうすればこの問題を解決できますか?