0

Good day. I have an android app where I allow the user to input their birthday into the edit texts.

Edit text has the following attributes:

<com.google.android.material.textfield.TextInputLayout
                style="@style/CustomOutlinedBox"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:boxBackgroundColor="@color/white"
                android:hint="Birthday MM/DD/YYYY">
                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/et_new_member_birthday"
                    android:textColor="@color/black"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:textSize="16sp"
                    android:inputType="date"/>
            </com.google.android.material.textfield.TextInputLayout>

How can I check if their input follows the mm/DD/yyyy format?

Editted: I tried adding the following block, unfortunately, it crashed my app.

private fun isDateValid(date: String): Boolean {
        val format = "MM/dd/yyyy"
        return try {
            val formatter = DateTimeFormatter.ofPattern(format)
            val convertedDate = LocalDate.parse(date, formatter)
            Toast.makeText(this, "Date is $convertedDate", Toast.LENGTH_SHORT).show()


            true
        } catch (e: ParseException) {
            Toast.makeText(this, "Error parsing date.", Toast.LENGTH_LONG).show()
            false
        }
    }

And I'm calling this method via Activity:

val etBirthday = binding.etNewMemberBirthday.text.toString()

...

TextUtils.isEmpty(etBirthday) || isDateValid(etBirthday)->{
                showErrorSnackBar(binding.root, "Please input a valid date.", true)
                false
            }
...
Jay Garzon
  • 41
  • 8
  • Maybe use a date picker type thing instead? Series of drop downs? Then you can ensure that the date passed matches regex pattern or something. – Peter Dec 14 '21 at 00:37
  • 2
    Possible duplicate of [Date checking](https://stackoverflow.com/questions/226910/how-to-sanity-check-a-date-in-java) ; Although, you are building Android GUIs, so maybe you should not use a text input for date picking : [Date picker](https://developer.android.com/guide/topics/ui/controls/pickers) – Dorian Naaji Dec 14 '21 at 00:39
  • One thing I hate about date picker is its too spacy. Don't worry this is just for testing purposes. This might come handy when time comes. I just want to know if this idea can be achieved. – Jay Garzon Dec 14 '21 at 00:40
  • 1
    In that case, just use a `DateTimeFormatter` or `SimpleDateFormat` with a suitable pattern to parse the date, and catch and report exceptions. – Stephen C Dec 14 '21 at 01:09
  • @StephenC I edit my post. I'll appreciate if you can check this out for me. – Jay Garzon Dec 14 '21 at 01:35
  • *"unfortunately, it crashed my app"* - provide details. For instance, a stack trace. (There are a bazzilion possible reasons why something might crash an app.) – Stephen C Dec 14 '21 at 01:39
  • 1
    Anyhow ... that is Kotlin, and I'm a Java person. It would be more appropriate if you didn't tag kotlin questions with `[java]`. – Stephen C Dec 14 '21 at 01:40
  • @StephenC I see. My bad. I can understand java. "In that case, just use a DateTimeFormatter or SimpleDateFormat with a suitable pattern to parse the date, and catch and report exceptions.". I just want to know how can I implement this one. Let's say perhaps extract this into a separate method where it returns a boolean value? I tried doing this in Kotlin but it's crashing. – Jay Garzon Dec 14 '21 at 01:49
  • Mate ... if you want someone to diagnose the crash, you need to show us the evidence. The stack trace for a start. – Stephen C Dec 14 '21 at 03:18
  • unfortunately, it crashed my app. - Please share the crash logs also. – Nitish Dec 14 '21 at 05:19

1 Answers1

1

You can simply use built in date picker dialog when ever user will click on edit text open date picker dialog user can easily select date and it can be of any pattern.


    et_new_member_birthday.setOnClickListener {
                val c = Calendar.getInstance()
                val year = c.get(Calendar.YEAR)
                val month = c.get(Calendar.MONTH)
                val day = c.get(Calendar.DAY_OF_MONTH)
    
                val dpd = DatePickerDialog(
                    mActivity,
                    R.style.DateDialogTheme,
                    { _, year, monthOfYear, dayOfMonth ->
                        val cal = Calendar.getInstance()
    
                        cal.set(Calendar.YEAR, year)
                        cal.set(Calendar.MONTH, monthOfYear)
                        cal.set(Calendar.DAY_OF_MONTH, dayOfMonth)
    
                        val pattern = "MM/dd/yyyy"
                        val simpleDateFormat = SimpleDateFormat(pattern)
                        val date = simpleDateFormat.format(cal.time)
                        et_new_member_birthday.setText(date)
                        
                    },
                    year,
                    month,
                    day
                )
                dpd.show()
    }

Ehsan Ullah
  • 144
  • 3