How to check if a string is entered by a user in the format mm / dd / yyyy in android?

In my application there is one text editing in which the user can enter a date in a format mm/dd/yyyy. I have to check if the entered value is in format mm/dd/yyyyor not. If not, I should show a text editing error.

How to do it?

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);
}
+4
source share
5 answers

You can use DateFormat as follows:

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;
    }
}
+1
source

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, for example:

isValidFormat("mm/dd/yyyy", "12/29/2016")
0
source

if(editText.getText().toString().matches("^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$"){
 //correct format
}else{
//invalid format
}
0

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

    m_Dob.setText(sdf.format(myCalendar.getTime()));
0

Since you use the Select Date dialog box to select a date, you can use this code to determine the 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. Good coding.

0
source

Source: https://habr.com/ru/post/1665193/


All Articles