Question: How to check whether string enter by user is in mm/dd/yyyy format in android?

Question

How to check whether string enter by user is in mm/dd/yyyy format in android?

Answers 5
Added at 2016-12-29 06:12
Tags
Question

There is one text edit in my app that the user can enter a date in the format mm/dd/yyyy. I have to check whether entered value is in mm/dd/yyyy format or not. If not I have to show edit text error.

How do I do this?

private final DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
    @SuppressLint("SetTextI18n")
    public void onDateSet(DatePicker view, int selectedYear,
                          int selectedMonth, int selectedDay) {
        m_Dob.setText((selectedMonth + 1) + "/" + selectedDay + "/" + selectedYear);
    }
};
m_dateBtn = (AppCompatImageButton) findViewById(R.id.date_Btn);
m_dateBtn.setOnClickListener(this);
Calendar cal = Calendar.getInstance();
day = cal.get(Calendar.DAY_OF_MONTH);
month = cal.get(Calendar.MONTH);
year = cal.get(Calendar.YEAR);

protected Dialog onCreateDialog(int id) {
    return new DatePickerDialog(this, datePickerListener, year, month, day);
}
Answers
nr: #1 dodano: 2016-12-29 06:12

You can use DateFormat like this :

public void isCorrectDateFormat(String value, String format)
    try {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        date = sdf.parse(value);
        return value.equals(sdf.format(date)));
    } catch (ParseException ex) {
        ex.printStackTrace();
        return false;
    }
}
nr: #2 dodano: 2016-12-29 06:12

try this method:

public static boolean isValidFormat(String format, String value) {
    Date date = null;
    try {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        date = sdf.parse(value);
        if (!value.equals(sdf.format(date))) {
            date = null;
        }
    } catch (ParseException ex) {
        ex.printStackTrace();
    }
    return date != null;
}

use this method like:

isValidFormat("mm/dd/yyyy", "12/29/2016")
nr: #3 dodano: 2016-12-29 06:12

Use this regex for date validation

if(editText.getText().toString().matches("^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$"){
 //correct format
}else{
//invalid format
}
nr: #4 dodano: 2016-12-29 06:12

use

    String myFormat = "mm/dd/yyyy";
    SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

    m_Dob.setText(sdf.format(myCalendar.getTime()));
nr: #5 dodano: 2016-12-29 07:12

Since your are using Date Picker dialog to select the date you can use this code to define your date format.

mEDDate.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick (View v){

    class MyDatePickerClass implements DatePickerDialog.OnDateSetListener {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

            int month = monthOfYear + 1;
            String formattedMonth = "" + month;
            String formattedDayOfMonth = "" + dayOfMonth;
            String formattedYear = String.valueOf(year);

            if (month < 10) {

                formattedMonth = "0" + month;
            }
            if (dayOfMonth < 10) {

                formattedDayOfMonth = "0" + dayOfMonth;
            }

            formattedYear = formattedYear.substring(formattedYear.length() - 2);
            String date = formattedDayOfMonth + "/" + formattedMonth + "/" + formattedYear;
            mEDDate.setText(date);

            String date = dayOfMonth + "/" + (monthOfYear + 1) + "/" + year % 100;

        }
    }

    DatePickerDialog datePickerDialog = new DatePickerDialog(GetMoreDiscountActivity.this, new MyDatePickerClass(), Calendar.getInstance().get(Calendar.YEAR) - 18, 0, 1);
    datePickerDialog.show();

}
});

Hope this help.Happy Coding.

Source Show
◀ Wstecz