解析日期并将其存储在SQLite数据库 [英] Parsing Date and storing it in SQLite database

查看:136
本文介绍了解析日期并将其存储在SQLite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我解析与SAXParser的一个XML文件,并在处理程序中,我的数据成员中的一个是日期创建对象。

I'm parsing an xml file with SAXParser and inside the handler I'm creating objects with one of the datamembers being the date.

在我的XML文件中的日期格式为: 2010-12-28

The date on my XML file is in this format: 2010-12-28.

我找不到如何把这样的字符串转换为Date对象,虽然。而且我也搞不懂怎么将其存储在一个SQLite数据库,因为似乎有多种格式(那些与时/分/等。)

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.

有人可以帮助我?

推荐答案

您可以使用Java的简单的日期格式的日期格式化成日期的对象,你可以用它来做计算,这样的:

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:

<一个href="http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html">http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html

你想要的解析字符串YYYY - MM - DD

至于其存储到SQLite数据库,你有两个选择:

As for storing it into the SQLite database, you have a couple of options:

  • 在储存它作为一个字符串,并将其转换回Date对象,当它出来
  • (preferred)将其存储为Unix时间戳( Date.getTime()给你的毫秒数自1970年以来,你可以存储在数据库中的值)。这种方法是preferred,因为你没有做那么多(相对昂贵)的转换。您可以在时间戳值做简单的基本操作。
  • 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.

有几个使用其他选项的SQLite内置的日期函数(如这里),但最终,如果你希望将其拉进了Android / Java的code和操纵它作为一个日期对象,你要去加时赛不得不做转换。

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.

这篇关于解析日期并将其存储在SQLite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆