Parsing Date and storing it in SQLite database

后端 未结 1 1377
谎友^
谎友^ 2021-01-07 03:06

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 f

相关标签:
1条回答
  • 2021-01-07 03:28

    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 the long 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.

    0 讨论(0)
提交回复
热议问题