0

I have an edit text field in app where user can insert time manually. I want to match proper date pattern like 1:00 AM - user can enter time in this format only if it's not in given format then it shows an error.

How can I make pattern matcher for this functionality?

halfer
  • 19,824
  • 17
  • 99
  • 186
Digvijay
  • 2,887
  • 3
  • 36
  • 86

1 Answers1

0

You can convert the user input time in a proper data time format. then you can match with the user-given time date-time format what you want. like this

SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm A",Locale.getDefault());
Calendar calendar1 = Calendar.getInstance();
Calendar calendar2 = Calendar.getInstance();

Date date1 = dateFormat.parse(editText.getText().toString());
Date date2 = dateFormat.parse(calendar2.getTime());

if (date1.after(date2) && date1.equals(date2)) {
//..do your work..//
}

This is an example code. It may not work. I just put this to give you the idea what I am saying

Md. Shofiulla
  • 2,135
  • 1
  • 13
  • 19