I want to compare two times(timestamp) in my android application. Here is my function to check whether the current time is between two given times.
private boolean isBetween(String t1, String t2, String target){
boolean result = false;
try{
Date time1 = new SimpleDateFormat("HH:mm:ss").parse(t1);
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(time1);
Date time2 = new SimpleDateFormat("HH:mm:ss").parse(t2);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(time2);
calendar2.add(Calendar.DATE, 1);
Date d = new SimpleDateFormat("HH:mm:ss").parse(target);
Calendar calendar3 = Calendar.getInstance();
calendar3.setTime(d);
calendar3.add(Calendar.DATE, 1);
Date x = calendar3.getTime();
if (x.after(calendar1.getTime()) && x.before(calendar2.getTime())) {
result = true;
//checkes whether the current time is between 14:49:00 and 20:11:13.
//Toast.makeText(UserCheckout.this, "The time is between", Toast.LENGTH_SHORT).show();
}
}
catch (ParseException e){
result = false;
Toast.makeText(UserCheckout.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
return result;
}
Here is how I am using this function.
if (isBetween("19:00:00", "20:00:00", currentTime)){
Toast.makeText(this, "Time is between 7 and 8", Toast.LENGTH_SHORT).show();
}
if (isBetween("20:00:00", "21:00:00", currentTime)){
Toast.makeText(this, "Time is between 8 and 9", Toast.LENGTH_SHORT).show();
}
But I am getting both the results true.