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