• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Using Date Picker to save Date into SQL Database

Arigead

Lurker
Nov 24, 2009
5
0
Hello All,
Where to start? Basically I used the example Date Picker in the resources Section:

Date Picker | Android Developers

To allow the user to enter a Date. I'm then saving this Date into my Database as a string. Basically when the date is selected the values of mYear, mMonth and mDay are updated. When a commit button is clicked I create a new java.sql.Date:

Date date = new Date(mYear, mMonth, mDay);

And store this date as a string in the data base (date.toString()).

All of the above works great but when I come to allow the user to edit the saved date I'm encountering problems. I pull the date out of the database as a string and fill this string into an intent to send to the Activity for Edit. In the Edit Activity the date.tostring is "3910-03-17" which is perfect as this seems to be how the date is stored for March 17th 2010.

Now I want to set this as the current date on the android date picker. date.getYear() and date.getMonth() both work but date.getDay() returns me the value "4" I've searched on the Internet and find that date.getDay() is deprecated as are many of the other methods.

Date | Android Developers

So following that page I try and use a Calendar object but I can't find a simple way to store a Calendar into a database and get it back.

Date date = Date.valueOf(extras.getString(MyDbAdapter.KEY_DATE));
Calendar cal;
cal.setTime(taxDate);

The above sets the year of the cal to 3910. I could start using maths to convert 3910 to 2010 but I'm assuming there is a better method?

I am saving the date in the data base as a string but you can easily go from date.toString and back again with date = Date.valueOf(string). You can't seem to be able to do this with a Calendar which can only go toString and not back. So I could save this string in the Database but how do I get it back?

Any advice would be very gratefully received.
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones