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?

Ojonugwa Jude Ochalifu
  • 26,627
  • 26
  • 120
  • 132
  • 3
    Why don't you use a [DatePicker](http://developer.android.com/reference/android/widget/DatePicker.html) instead of over-complicating with an `EditText`? The user experience is enriched and your life is easier! – gunar Feb 04 '14 at 22:02
  • This is a duplicate of this question http://stackoverflow.com/questions/999172/how-to-parse-a-date – jeff_kile Feb 04 '14 at 22:03
  • Because enriching user experience is so mainstream :D – Ojonugwa Jude Ochalifu Feb 04 '14 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. – Ojonugwa Jude Ochalifu Feb 04 '14 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. – Prakash Feb 04 '14 at 22:27

6 Answers6

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

gipinani
  • 14,038
  • 12
  • 56
  • 85
  • 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. – danny117 Feb 04 '14 at 22:05
  • Possibly @mserioli its an ORM (I had to look ORM up) – danny117 Feb 04 '14 at 22:12
1
this.dateOfBirth = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse(dateOfBirthTextField);
wawek
  • 1,577
  • 1
  • 13
  • 23
1
String dateOfBirth = dateOfBirthTextField.getText().toString();
SimpleDateFormat s=new SimpleDateFormat("MM/dd/yyyy"); // use your pattern here
Date d = s.parse(dateOfBirth);
elbuild
  • 4,869
  • 4
  • 24
  • 31
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.

Semih Eker
  • 2,389
  • 1
  • 20
  • 29
1

click here to see an example by mkyong

Mohsin AR
  • 2,998
  • 2
  • 24
  • 36
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);
chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
mavalan
  • 21
  • 2