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