-3

I want to calculate restaurant's opened hours. I have two string, for example:

String start_hour = "09:00";
String end_hour = "18:00";

And current hour for example:

    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;
        }

But I have some problem with that. This method working wrong when start_hour is "13:00" and end_hour "05:00". Because 05:00 hour from next day. How can I solve this problem?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Benfactor
  • 543
  • 4
  • 11
  • 31
  • I recommend you don’t use `Calendar`, `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, `SimpleDateFormat` in particular notoriously troublesome. Instead use `LocalTime` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Dec 19 '19 at 20:02

2 Answers2

1

instead of

nowh.before(enh) && nowh.after(sth)

use

nowh.before(enh) && nowh.after(sth) && sth.before(enh)
|| enh.before(sth) && !(nowh.before(enh) && nowh.after(sth))

Apart from that I think Calendar class is supposed to be used differently I assume...

Alex
  • 521
  • 7
  • 17
1

java.time and ThreeTenABP

public boolean isRestaurantOpenNow() {
    LocalTime startHour = LocalTime.parse("13:00");
    LocalTime endHour = LocalTime.parse("05:00");

    LocalTime currentHour = LocalTime.now(ZoneId.systemDefault());
    if (startHour.isBefore(endHour)) { // Both are on the same day
        return currentHour.isAfter(startHour) && currentHour.isBefore(endHour);
    } else { // end is on the next day
        return currentHour.isBefore(endHour) || currentHour.isAfter(startHour) ;
    }
}

Trying it just now (21:20 in my time zone):

    System.out.println(isRestaurantOpenNow());

Output was:

true

Question: Doesn’t java.time require Android API level 26?

java.time works nicely on both older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161