javaandroidtimepickerdialog

java.text.ParseException: Unparseable date: "" (at offset 0) error for timepickerdialog


Hi this is my first time developing an simple Android-based application. I need to validate my starting time and ending time, which means ending time must not be less than or equal to starting time. I'm using an EditText to prompt a timepicker dialog. I had tried this code but it doesn't work, in terms of getting the error above at the line below

Date endTimeDate = format.parse(inputEndTime.getText().toString());

This is the whole code of the OnClickListener for EditText field to prompt out a timepicker dialog. I even tried to reverse the statements in if-else but it doesn't work for me too. Anyone can help me in this. Really thanks a lot!

inputEndTime.OnClickListener code:

inputEndTime.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v)
        {
            int hour = myTime.get(Calendar.HOUR_OF_DAY);
            int min = myTime.get(Calendar.MINUTE);
            TimePickerDialog myEndTimePickerDialog = new TimePickerDialog(ViewDocActivity.this,
                    new TimePickerDialog.OnTimeSetListener() {
                        @Override
                        public void onTimeSet(TimePicker view, int hourOfDay, int minute)
                        {
                            SimpleDateFormat format = new SimpleDateFormat("HH:mm");
                            try
                            {
                                Date startTimeDate = format.parse(inputTime.getText().toString());
                                Date endTimeDate = format.parse(inputEndTime.getText().toString());

                                if (startTimeDate.compareTo(endTimeDate) <= 0)
                                {
                                    Context timeContext = getApplicationContext();
                                    CharSequence text = "Please enter the correct end time";
                                    int duration = Toast.LENGTH_LONG;

                                    Toast toast = Toast.makeText(timeContext, text, duration);
                                    toast.show();
                                }
                                else
                                {
                                    inputEndTime.setText(String.format("%02d:%02d", hourOfDay, minute));

                                }
                            }
                            catch(ParseException e)
                            {
                                e.printStackTrace();
                            }
                        }
                    }, hour, min, true);

            myEndTimePickerDialog.setTitle("Select Time");
            myEndTimePickerDialog.show();
        }
    });

Solution

  • The reason for your error is, that you are trying to parse the time from your EditText (inputEndTime), but that is empty at the time you do the format.parse().

    As you set this up, you have an EditText and a TimePickerDialog. Now you are implementing the TimePickerDialog.OnTimeSetListener#onTimeSet() method. Here you get the time, the user selected in the dialog via the hourOfDay and minute parameters of the method. At this point you have the time, but it not yet written in the EditText field itself.

    So the simplest solution to get your code working would be to set that time in your EditText field before doing anything further. To do so, add the following line as the first line of the onTimeSet() method:

    inputEndTime.setText(String.format("%02d:%02d", hourOfDay, minute));
    

    This sets the picked time as text in the EditText field in a format that can then be parsed with format.parse(...) later on.