解析日期并将其存储在 SQLite 数据库中
我正在使用 SAXParser 解析 xml 文件,并在处理程序内创建对象,其中数据成员之一是日期。
我的 XML 文件上的日期采用以下格式:2010-12-28
。
但我找不到如何将这样的字符串转换为 Date 对象。我也不明白如何将它存储在 SQLite 数据库中,因为似乎有很多格式(带有小时/分钟/等的格式)
并且我需要将它存储在一个对象中,以便我可以计算时间跨度等。
有人可以吗帮我解决这个问题吗?
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 need it stored in an object so I can calculate timespans etc.
Can someone help me with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 Java 的简单日期格式将日期格式化为
Date
对象,您可以使用该对象进行计算等:http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html
您想要的解析字符串是
"yyyy'- 'MM'-'dd"
至于将其存储到 SQLite 数据库中,您有几个选择:
Date.getTime()
为您提供自 1970 年以来的毫秒数,您可以将该值存储在数据库中。)这种方式是首选,因为您不必这样做许多(相对昂贵的)转换。您可以简单地对long
时间戳值执行基本操作。还有一些使用 SQLite 内置日期函数的其他选项(如此处所述),但最终如果您想将其拉入 Android/Java 代码并将其作为
Date
对象进行操作,则无需进行转换。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:
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.