2

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.

Hanry
  • 5,481
  • 2
  • 40
  • 53
  • Have a look at `calendar2.add(Calendar.DATE, 1);` and `calendar3.add(Calendar.DATE, 1);`, because I think you're checking if `currentTime` is between 19:00 today and 20:00 tomorrow, and 20:00 today and 21:00 tomorrow, respectively. – Jeroen Steenbeeke Apr 16 '18 at 08:32
  • 1
    What is the use of `calendar2.add(Calendar.DATE, 1);` and `calendar3.add(Calendar.DATE, 1);`? Does the API version you're coding against support Java 8? If so, maybe use Java's time api. – M. le Rutte Apr 16 '18 at 08:36
  • @JeroenSteenbeeke Thanku so much man. it worked. – Jagdish Choudhary Apr 16 '18 at 08:37
  • 1
    @M.leRutte the question is flagged as Android so Java 8 features come with some caveats. That said, those APIs are probably a much better choice for tackling this problem, so using those or the ThreeTen library are indeed a much better idea – Jeroen Steenbeeke Apr 16 '18 at 08:39
  • The hack is of course just to compare the strings using `String.compareTo`. It won’t be very readable, but it will work. – Ole V.V. Apr 16 '18 at 10:45
  • It seems you took the code from [this answer](https://stackoverflow.com/a/17698079/5772882). Did you read the comments to the answer too? “This probably gives the wrong answer if someRandomTime lies between string1 and "00:00:00".” And: “its showing 11:44 is inbetween 19:28 to 23:59”. Fortunately there are 17 other answers to the same question, so I think you can find something that works. – Ole V.V. Apr 16 '18 at 14:37

1 Answers1

3

By calling:

calendar2.add(Calendar.DATE, 1);
calendar3.add(Calendar.DATE, 1);

you are adding a day to these dates. You are checking if your 'target' date (transposed forward by a day) is between "19:00:00" and "20:00:00 (tomorrow)" and "20:00:00" and "21:00:00 (tomorrow)"

Change your code to :

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);


        Date d = new SimpleDateFormat("HH:mm:ss").parse(target);
        Calendar calendar3 = Calendar.getInstance();
        calendar3.setTime(d);


        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;
}
KYHSGeekCode
  • 1,068
  • 2
  • 12
  • 30
fuzzKitty
  • 334
  • 3
  • 7