0

Am trying to store a date object in Android.I want to get the date as a String from EditText and then "convert" or should i say store it as a Date object.This is what i have done.

I have a TextField

`EditText dateOfBirthTextField = (EditText) findViewById(R.id.DoBTextField);`

and then a String

`String dateOfBirth = dateOfBirthTextField.getText().toString();`

Now i have a Student class that has a dateOfBirth field of type Date and a method

`setDateOfBirth(Date dob){
 this.dateOfBirth=dob;
 }

How do i set the value of dateOfBirth with what ever is entered into dateOfBirthTextField?

5
  • 3
    Why don't you use a DatePicker instead of over-complicating with an EditText? The user experience is enriched and your life is easier! Commented Feb 4, 2014 at 22:02
  • This is a duplicate of this question stackoverflow.com/questions/999172/how-to-parse-a-date Commented Feb 4, 2014 at 22:03
  • Because enriching user experience is so mainstream :D Commented Feb 4, 2014 at 22:10
  • @gunar am just working on something little with this.I just gave DatePicker a look and i think it makes more sense. Commented Feb 4, 2014 at 22:19
  • Remember SimpleDateFormat construction is expensive, many had performance issue with it. And secondly it is not Thread Safe so use wisely! in your case I would go with DatePicker if UI allow. Commented Feb 4, 2014 at 22:27

6 Answers 6

3
Date date = new SimpleDateFormat("yyyy/MM/dd").parse (yourStringDate);

then call

setDateOfBirth(date);

You can provide your custom format to constructor.

Take a look at SimpleDateFormat javadoc: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Sign up to request clarification or add additional context in comments.

2 Comments

the native sqllite database only supports string so you'll probably be saving a string. So once its validated as a date save the data in the format @mserioli has provided.
Possibly @mserioli its an ORM (I had to look ORM up)
1
this.dateOfBirth = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse(dateOfBirthTextField);

Comments

1
String dateOfBirth = dateOfBirthTextField.getText().toString();
SimpleDateFormat s=new SimpleDateFormat("MM/dd/yyyy"); // use your pattern here
Date d = s.parse(dateOfBirth);

Comments

1

You can do sthg like that;

 String str = "26/08/1994";
 SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");                         
 Date date = formatter.parse(str);

please notice the capital M.

Comments

1

click here to see an example by mkyong

Comments

1

I'm assuming you meant to declare a string dateOfBirthString and a date dateOfBirth. You just convert the string to a date with a SimpleDateFormat.

It should be something like this:

EditText dateOfBirthTextField = (EditText) findViewById(R.id.DoBTextField);
String dateOfBirthString = dateOfBirthTextField.getText().toString();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); //you have to enter the format of your string, here "dd/MM/yyyy" = "day/month/year"
Date dateOfBirth = sdt.parse(dateOfBirthString);
setDateOfBirth(dateOfBirth);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.