0

I am developing an Android application, one of the features I have developed is to check whether a tenant is open or not, I use the code below

                SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
                final Date timeClose, timeOpen;
                try {
                    timeClose = format.parse(data.getClose());
                    timeOpen = format.parse(data.getOpen());
                    Calendar c1 = Calendar.getInstance();
                    c1.setTime(timeOpen);
                    c1.add(Calendar.DATE, 1);
                    Calendar c2 = Calendar.getInstance();
                    c2.setTime(timeClose);
                    c2.add(Calendar.DATE, 1);

                    Calendar c3 = Calendar.getInstance();
                    c3.setTime(currentTime());
                    c3.add(Calendar.DATE, 1);
                    Date x = c3.getTime();
                    if (x.after(c1.getTime()) && x.before(c2.getTime())){
                        Intent in = new Intent(context, ListMenuActivity.class);
                        in.putExtra("ID_TENANT",data.getKodeForm());
                        in.putExtra("NM_TENANT", data.getNm_tenant());
                        in.putExtra("IMG_TENANT", data.getGambar());

                        in.putExtra("LAT_TENANT", String.valueOf(data.getLat()));
                        in.putExtra("LON_TENANT", String.valueOf(data.getLon()));
                        context.startActivity(in);
                        Log.d("CURRENT_TIMES", String.valueOf(currentTime().compareTo(timeClose)));
                        Log.d("CURRENT_TIMESS", String.valueOf(currentTime().compareTo(timeOpen)));
                    } else {
                        Toast.makeText(context, "Maaf, Restoran ini masih tutup", Toast.LENGTH_SHORT).show();
                       
                        return;
                    }


                } catch (ParseException e) {
                    e.printStackTrace();
                    Log.e("ERROR_PARSE_DATA", e.getMessage());
                }

in the code above the logic is appropriate if the time is still in one day or date, but the problem appears when the day is different, for example the tenant opens at 22:00:00 and closes the next day at 03:00:00. is there a way to solve the problem? please help me

  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends. See if you either can use [desugaring](https://developer.android.com/studio/write/java8-support-table) or add [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project, in order to use java.time, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Sep 17 '20 at 19:23
  • Does this answer your question? [Check if a given time lies between two times regardless of date](https://stackoverflow.com/questions/17697908/check-if-a-given-time-lies-between-two-times-regardless-of-date). I recommend [the answer by Basil Bourque](https://stackoverflow.com/a/39712175/5772882) using java.time. – Ole V.V. Sep 17 '20 at 19:26
  • Also if you can, boil down your code to [a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). In case anyone had been going to write a new answer, it would be nice to be able to copy-paste your code, run it and see that it works for times on the same day and in which way it fails if the tenant closes on the next day. – Ole V.V. Sep 18 '20 at 05:01

1 Answers1

0

let me share to you a method that I did to check if my app is on trial period or not:

private boolean isInTrialPeriod() {
    TrialPeriodPreferences trialPeriodPreferences = new TrialPeriodPreferences(context);
    Long firstTimeDateTime = trialPeriodPreferences.getLong(TrialPeriodPreferences.FIRST_TIME_DATE_TIME);
    Calendar endTrialDate = Calendar.getInstance();
    endTrialDate.setTimeInMillis(firstTimeDateTime);
    endTrialDate.add(Calendar.DATE, 15);
    Calendar currentDate = Calendar.getInstance();
    if (currentDate.getTimeInMillis() > endTrialDate.getTimeInMillis())
        return false;
    return true;
}

Adding more information, in my case, I saved the date when this app was opened for first time in shared prefs, then I took this time and I added it 15 days. Finally, I got current time and I compared it.