How to take any time input from user using speech recognizer

Viewed 139

Basically I am creating one reminder application for visually impaired peoples so for reminder I am taking time , date and task that blind peoples want to remember but problem is it works fine for date input. But when user say any time let say 11:12 PM it correctly format this time in HH:mm:a but when time is 9:03 PM then it takes as 93 PM. Also when time is 1:00 PM. but it takes it as 1 PM which is wrong. I don't know whether this problem with speech recognizer or with Date time formatter.

My code:-

            String time = result.get(0);

            time = time.replace("a.m", "AM");
            time = time.replace("p.m", "PM");
            time = time.replace("a.m.", "AM");
            time = time.replace("p.m.", "PM");

            if (time.contains("12") && time.contains("PM")) {
                try {
                    String parsePatter = time.contains(":") ? "hh:mm a" : "hh m a";
                    SimpleDateFormat parser = new SimpleDateFormat(parsePatter);
                    Date date1 = parser.parse(time);
                    SimpleDateFormat resultParse = new SimpleDateFormat("hh:mm a");
                    String result1 = resultParse.format(date1);
                    result1 = result1.replace("pm", "PM");
                    mTimebtn.setText(result1);
                    finaltime = mTimebtn.getText().toString();
                } catch (Exception ex) {
                    Toast.makeText(getApplicationContext(), ex.getMessage(), Toast.LENGTH_SHORT).show();
                    layoutclick = 1;
                }
            }
            if (time.contains("AM")) {
                try {
                    String parsePatter = time.contains(":") ? "hh:mm a" : "hh m a";
                    SimpleDateFormat parser = new SimpleDateFormat(parsePatter);
                    Date date1 = parser.parse(time);                 
                    SimpleDateFormat resultParse = new SimpleDateFormat("hh:mm a");
                    String result1 = resultParse.format(date1);
                    result1 = result1.replace("am", "AM");
                    mTimebtn.setText(result1);
                    finaltime = mTimebtn.getText().toString();
                } catch (Exception ex) {
                    Toast.makeText(getApplicationContext(), ex.getMessage(), Toast.LENGTH_SHORT).show();
                    layoutclick = 1;

                }
            }
            if (result.get(0).contains("AM") && result.get(0).length()==5) {
                //check if time in 9:09 AM format
                // This logic is not working.
                try {
                    SimpleDateFormat sdf = new SimpleDateFormat("h:mm");
                    Date dateObj = sdf.parse(time);
                    String result1 = new SimpleDateFormat("HH:mm").format(dateObj);
                    mTimebtn.setText(result1);
                    finaltime = mTimebtn.getText().toString();
                } catch (final ParseException e) {
                    Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                    layoutclick = 1;
                }
            }
            if (time.contains("PM")) {
                try {
                    String parsePatter = time.contains(":") ? "hh:mm a" : "hh m a";
                    SimpleDateFormat parser = new SimpleDateFormat(parsePatter);
                    Date date1 = parser.parse(time);
                    SimpleDateFormat resultParse = new SimpleDateFormat("HH:mm a");
                    String result1 = resultParse.format(date1);
                    result1 = result1.replace("pm", "PM");
                    mTimebtn.setText(result1);
                    finaltime = mTimebtn.getText().toString();
                } catch (Exception ex) {
                    Toast.makeText(getApplicationContext(), time, Toast.LENGTH_SHORT).show();
                    textToSpeech.speak("time is wrong.",TextToSpeech.QUEUE_FLUSH,null);
                    layoutclick = 1;
                }
            }

I have used layoutclicked variable for increasing number of clicks on the layout.

1 Answers

You will have to build a logic to take the time as input, let me tell you a logic which I developed.

When the time is only single digit .i.e. 1 -> add :00 after 1 when the time is in two digits but has no ':' in it .i.e. 93 -> separate the digits and add : and 0 in between.

You will have to build few logics to get the time correct.

Hope it helps...

Related