0

I am writing an Android app and I need to know if a store is open during a specific range of time or not. I already using the code below:

try {
        Date time1 = new SimpleDateFormat("HH:mm:ss").parse(getResources().getString(R.string.opening_time));
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(time1);

        Date time2 = new SimpleDateFormat("HH:mm:ss").parse(getResources().getString(R.string.closing_time));
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(time2);
        calendar2.add(Calendar.DATE, 1);


        Date current = new SimpleDateFormat("HH:mm:ss").parse(getCurrentTime());
        Calendar calendar3 = Calendar.getInstance();
        calendar3.setTime(d);
        calendar3.add(Calendar.DATE, 1);

        Date x = current.getTime();
        if (x.after(calendar1.getTime()) && x.before(calendar2.getTime())) {
            isOpened = true;
        }
    } catch (ParseException e) {
        e.printStackTrace();
    } finally {
        return isOpened;
    }

The issue with this code is that he is not taking account the timezone.

time1 and time2 are respectively opening and closing time and they are based on a PST timezone. right now they are defined as below:

<string name="opening_time">06:00:00</string>
<string name="closing_time">18:00:00</string>

they are based on a 24h format and not specifying the timezone. as the opening time is always based on the same timezone, I can setup programmatically if needed.

How can I make it work correctly to reply true or false if the store is open and take into account the time zone. As my store is opened from 6am to 6pm PST, if someone try to enter at 8pm EST, it should be good.

I have a minimum support for API 19

Thanks

Seb
  • 2,929
  • 4
  • 30
  • 73
  • When you say PST, do you mean UTC offset -08:00 or do you mean America/Los_Angeles time zone? Most of the year the two are not the same. – Ole V.V. Jun 11 '18 at 19:15

2 Answers2

0

if you set you time to UTC will be trhe same for all time zones try something like that :

   SimpleDateFormat SM_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS")
   SM_DF_POST_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
librushi
  • 81
  • 2
  • 6
0

It’s easiest to keep the whole calculation in the store’s timezone so you don’t need any conversions between timezones:

import org.threeten.bp.LocalTime;
import org.threeten.bp.ZoneId;

public class IsStoreInOtherTimeZoneOpen {

    public static void main(String[] args) {
        ZoneId storeTimeZone = ZoneId.of("America/Los_Angeles");

        LocalTime openingTime = LocalTime.parse("06:00:00");
        LocalTime closingTime = LocalTime.parse("18:00:00");
        LocalTime nowInStore = LocalTime.now(storeTimeZone);
        boolean isOpen = nowInStore.isAfter(openingTime) && nowInStore.isBefore(closingTime);

        System.out.println("Is store open? " + isOpen);
    }

}

I ran this code just now on my computer. Local time (Europe/Copenhagen time zone) was 21:31, equal to 12:31 in Los Angeles. The snippet printed:

Is store open? true

The code is independent of the device timezone as long as the device clock is going correctly.

I am using and recommending java.time, the modern Java date and time API.

Question: Can I use java.time on Android API level 19?

Yes, java.time works nicely on 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, I’m told) the modern API comes built-in. In this case import java.time.LocalTime and java.time.ZoneId (not the classes from org.threeten.bp i am using above).
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new 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. I have been told that dependencies to use is compile group: 'org.threeten', name: 'threetenbp', version: '1.3.3', classifier: 'no-tzdb'.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • thanks @ole-v-v I try this code, but it still required API26. My minSDK is API19 and the parse and ZoneId.of are not supported if your minSDK is API 19 – Seb Jun 12 '18 at 16:12
  • Did you add ThreeTehABP to your project as I said and import `org.threeten.bp.ZoneId` and `org.threeten.bp.LocalTime`? I have tested my code on ThreeTen Backport, so I firmly believe it will work on ThreeTenABP too. And I have edited the answer with a few more details to help you out. – Ole V.V. Jun 13 '18 at 07:04