title: How to create object with a possibly empty date?
body: <p>I have a class User which has: firstName:String, lastName:String, age:Int?, and dob:Date?. The values I am getting from editText boxes from the activity, straight forward.</p>
  <p>None of the fields are actually required, some or all of them might be empty.</p>
  <p>That's what I've got so far:</p>
  <pre><code>private fun insertDataToDatabase() {
      val firstName = addFirstName_et.text.toString()
      val lastName = addLastName_et.text.toString()
      val age = addAge_et.text
      val dob = addDob_et.text
  
      val user = User(
                  firstName,
                  lastName,
                  age.toString().toIntOrNull(),
                  Date.valueOf(dob.toString())
              )
              mUserViewModel.addUser(user)
  }
  </code></pre>
  <p>As you can see from the insertToDatabase function I'm turning the firstName and lastName from the editTexts to String via .toString, and the age and dob are just editables from the .text</p>
  <p>Now as mentioned, all the fields are allowed to be empty. With the firstName and lastName that is not an issue, they can simply remain empty and the creation of the user object works with entries of &quot;&quot;.</p>
  <p>Also the age works since the .toIntOrNull() on the string I return from age.toString() returns null which works for creating the user object in case the age editText is empty.</p>
  <p>But how do I do that for the dob which needs to be of type Date in the User class?</p>
  <p>When passing the variables for the creation of the user object I would like to check if the editable dob is empty, then return null, if it's not empty return the editable with .toString() and turn it into a Date with .valueOf. Basically a similar thing as with the age one line above, but with Date instead of String.</p>
  <p>The code I have doesn't work.</p>
  <p>I hope you get the idea of what I mean, sorry I am a beginner.</p>
  <p>Any ideas? Thanks a lot!!</p>