I'm parsing an xml file with SAXParser and inside the handler I'm creating objects with one of the datamembers being the date.
The date on my XML file is in this format: 2010-12-28
.
I can't find how to turn a string like that into a Date object though. And I also don't understand how to store it in a SQLite database, because there seem to be many formats (ones with hour/minutes/etc.)
And I nee开发者_开发问答d it stored in an object so I can calculate timespans etc.
Can someone help me with this?
You can use Java's Simple Date Format to format your date into a Date
object that you can use to do calculations and such:
http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html
The parse string you want is "yyyy'-'MM'-'dd"
As for storing it into the SQLite database, you have a couple of options:
- Store it as a string and convert it back to a date object when it comes out
- (Preferred) Store it as a unix timestamp (
Date.getTime()
gives you the number of milliseconds since 1970, and you can store that value in the DB.) This way is preferred because you don't have to do so many (relatively expensive) conversions. You can simple do basic operations on thelong
timestamp values.
There are a couple of other options using SQLite's built in date functions (as described here), but ultimately if you want to pull it into the Android/Java code and manipulate it as a Date
object you're going ot have to do a conversion.
精彩评论