0

I am building an app, and I need to know how to compare two hours.

For example, my store opens at 6:30 AM and closes at 5:00 PM.

I need to show in a TextView that after 5 PM my store is closed, and after 6:30 AM my store is open, from Monday to Friday.

How can I do this? This is my attempt:

public void timer() {
    Calendar c = Calendar.getInstance();
    int timeOfDay = c.get(Calendar.HOUR_OF_DAY);
    int open = 6:30; // 1 ERROR HERE WITH ":"
    int close = 17;

    if (timeOfDay < close) {
        hour.setText.("OPEN");
    }
}

But I am getting multiple errors. One in int open when I put 06:30 with ":"; two with limit of Monday to Friday.

Thanks for the help.

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
Matt
  • 39
  • 1
  • 6
  • There is no date literals in Java. I suggest using an `java.time.Instant` – killjoy Aug 20 '18 at 20:34
  • 4
    I suggest using the new java.time packages. You can use it on Android with this library https://github.com/JakeWharton/ThreeTenABP . Reasons see https://stackoverflow.com/questions/48688736/are-java-util-date-and-java-util-calendar-deprecated – leonardkraemer Aug 20 '18 at 21:00

5 Answers5

9

You should be using the Java 8 Date & Time library, because Calendar and Date are obsolete.

The LocalTime class should be sufficient.

LocalTime open = LocalTime.of(6, 30);
LocalTime closed = LocalTime.of(17, 0);

LocalTime currentTime = LocalTime.now();
if (currentTime.isBefore(open) || currentTime.isAfter(closed)) {
    // Closed
}

You could then use the DateTimeFormatter class to format the time into the desired format.

If you want to also take the day of the week into consideration while determining the opening times of your shop, then you could use LocalDateTime in conjunction with abovementioned example:

LocalDateTime now = LocalDateTime.now();

// The opening days. Static import these from java.time.DayOfWeek
Set<DayOfWeek> daysOpen = new HashSet<>(Arrays.asList(
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY));
// Opening and closing times
LocalTime timeOpen = LocalTime.of(6, 30);
LocalTime timeClosed = LocalTime.of(17, 0);

if (!daysOpen.contains(now.getDayOfWeek()) || now.toLocalTime().isBefore(timeOpen) || now.toLocalTime().isAfter(timeClosed)) {
    System.out.println("Closed");
}
else {
    System.out.println("Open");
}
MC Emperor
  • 22,334
  • 15
  • 80
  • 130
  • 4
    For Android before API level 26 you need this: https://github.com/JakeWharton/ThreeTenABP – leonardkraemer Aug 20 '18 at 21:05
  • :( not work show me this `Failed resolution of: Ljava/time/LocalTime`, and i follow steps from link and does not work either – Matt Aug 21 '18 at 15:34
  • Java 8 is supported from API level 26. If you are targeting a lower API level, then you could use Joda Time instead. [More info here](https://stackoverflow.com/questions/15675173/adding-joda-time) or [here](https://stackoverflow.com/questions/29237563/adding-joda-time-to-android-studio). – MC Emperor Aug 21 '18 at 17:17
  • Or just include ThreeTenAPB, as @leonardkraemer mentioned. – MC Emperor Aug 21 '18 at 17:26
0

The constant HOUR_OF_DAY is an integer representing only the hour. I would suggest to use the method getTime() (see this link) and then work with the returned Date object.

0

I couldn't think of a very concise way to do this, but perhaps you can use this method

public boolean isOpen(Calendar c) {
   int minute = c.get(Calendar.MINUTE);
   int hour = c.get(Calendar.HOUR_OF_DAY);
   return (hour < 17 && (hour > 6 || (hour == 6 && minute >=30)));
}

This should return true if it is between 6:30 and 17:00 and false at any other time

(If the hour is less than 17, and (the hour is greater than 6, or the hour is 6 and the minutes are 30 or higher))

AAvery
  • 128
  • 6
-1

The HOUR_OF_DAY field works like this.

10:04:15 PM the HOUR_OF_DAY is 22.

You can't get 6:30 am because it only tells you what hour it is not minute.

You can get minutes by calling MINUTE.

A few changes would be:

public void timer () {
    Calendar c = Calendar.getInstance();
    int timeOfDay = c.get(Calendar.HOUR_OF_DAY);
    int minuteOfHour = c.get(Calendar.MINUTE); -> same as Hour
    int open = 6; -> you shouldn't use ':' with an int
    int openMinutes = 30;
    int close = 17;

    if(timeOfDay >= open && minuteOfHour >= openMinutes && timeOfDay < close){
        hour.setText.("OPEN");
    }}
Yug Singh
  • 3,112
  • 5
  • 27
  • 52
  • 1
    Excuse me, your answer demonstrates very well why the `Calendar` class was given up and replaced by java.time (the modern Java date and time API). If time now is 8:05 in the morning, `minuteOfHour` will be less than `openMinutes`, and your store will show as closed. – Ole V.V. Aug 20 '18 at 21:16
  • These terrible date-time classes were supplanted by the modern *java.time* classes. – Basil Bourque Aug 22 '18 at 02:02
-1

Use following code:

        Calendar openingHour = Calendar.getInstance();
        openingHour.set(Calendar.HOUR_OF_DAY, 6);
        openingHour.set(Calendar.MINUTE, 30);
        openingHour.set(Calendar.SECOND, 0);
        openingHour.set(Calendar.MILLISECOND, 0);

        Calendar closingHour = Calendar.getInstance();
        closingHour.set(Calendar.HOUR_OF_DAY, 17);
        closingHour.set(Calendar.MINUTE, 0);
        closingHour.set(Calendar.SECOND, 0);
        closingHour.set(Calendar.MILLISECOND, 0);

        Calendar currentTime = Calendar.getInstance();
        int dayOfWeek = currentTime.get(Calendar.DAY_OF_WEEK);
        if( dayOfWeek % 7 > 1  && currentTime.before(closingHour) && currentTime.after(openingHour)){
           System.out.println("Store is open");
        } else {
           System.out.println("Store is closed");
        }
Mahdi Rajabi
  • 582
  • 1
  • 4
  • 11
  • Work fine :), but how i can do for that work in the two format hour thnks – Matt Aug 20 '18 at 21:08
  • what do you mean by two hour format? give me sample output – Mahdi Rajabi Aug 20 '18 at 21:09
  • For example, my phone use format 00 to 12 where the 17:00pm will be 5:00pm in another phone, the format is 24, so 17:00 is the 17:00 – Matt Aug 20 '18 at 21:16
  • It's not related to your device time format, whether it's 24 hour format or 12 format. when you get HOUR_OF_DAY from calendar, it returns you time in 24 format ;) if you want string formatting, don't hesitate to tell me to send you code snippet to format your out put :) – Mahdi Rajabi Aug 20 '18 at 21:21
  • how i could change this 17, you konow here in colombia is 4:pm = 14:00 and show closed jejeje, because in code y put 17, and 04 is before that open – Matt Aug 20 '18 at 21:24
  • in Columbia 4:pm is 14:00? must not be 16:00? anyway, no need to change, calendar class handles everything by itself, it's only a presentation and time object works in background, if you want set it in the code in 12 hour format use closingHour.set(Calendar.HOUR, 4) following with closingHour.set(Calendar.AM_PM, 1); – Mahdi Rajabi Aug 20 '18 at 21:34
  • I am afraid that this will show the store as closed on Mondays. Using the obsolete `Calendar` class correctly isn’t that easy… – Ole V.V. Aug 20 '18 at 21:35
  • I've just edit it not be afraid, dayOfWeek % 7 > 1 this would omit cause of frightening. Saturday is 7 and Sunday is 1, so it works correctly – Mahdi Rajabi Aug 20 '18 at 21:40