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