0

I have a time interval - 12:00 PM to 11:59 PM.
When the user enters the time as "06:15 PM", check against that interval.
If the time falls into that interval, return true else return false.
I searched for thisin Google, but I didn't get the correct solution.
Can anyone help me to solve this issue?

 String From = "12:00 PM";
 String to = "11:59 PM";

 String user_given_input = "06:15 PM";
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Yugesh
  • 4,030
  • 9
  • 57
  • 97

2 Answers2

3

You could parse it into a Date object using a SimpleDateFormatter. After you've done that you can use fromDate.before(user_given_input_date) && toDate.after(user_given_input_date). assuming fromDate, toDate & user_given_input_date are Date objects

0xDEADC0DE
  • 2,453
  • 1
  • 17
  • 22
  • in this `SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");` or `SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");` – Yugesh Jun 03 '14 at 13:07
  • According to the `SimpleDateFormat` documentation (http://developer.android.com/reference/java/text/SimpleDateFormat.html) it should be `new SimpleDateFormat("hh:mm a") as you stated` – 0xDEADC0DE Jun 03 '14 at 13:15
2

Try this code, it should work.

static boolean checkTimeInterval(String s) {
        String From = "12:00 PM";
        String to = "11:59 PM";
        SimpleDateFormat d = new SimpleDateFormat("hh:mm a");
        boolean isLie = false;
        try {
            Date from = d.parse(From);
            Date too = d.parse(to);
            Date input = d.parse(s);

            if (input.before(too) && input.after(from)) {
                isLie = true;
            }

        } catch (ParseException e) {
            System.out.println("Check input string format");
            e.printStackTrace();
        }

        return isLie;

    }

You may call this function as

checkTimeInterval("06:15 PM")
Mukesh Kumar Singh
  • 4,512
  • 2
  • 22
  • 30