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

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

问题描述

我正在使用SAXParser解析一个xml文件,并在处理程序内部创建对象,其中一个datamembers是日期。



我的XML上的日期文件采用以下格式: 2010-12-28



我找不到如何转一个字符串像这样的一个Date对象。而且我也不明白如何将它存储在SQLite数据库中,因为似乎有很多格式(小时/分钟/等等)。



而且需要它存储在一个对象中,所以我可以计算timespans等。



有人可以帮助我吗?

解决方案

您可以使用Java的简单日期格式将日期格式化为可用于进行计算的 Date 对象, p>

http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html



解析你想要的字符串是yyyy' - 'MM' - 'dd



至于将其存储到SQLite数据库,您有几个选项:




  • 将其作为字符串存储,并将其转换为日期对象, li>
  • (首选) Sto将它作为unix时间戳记( Date.getTime()提供自1970年以来的毫秒数,您可以将该值存储在数据库中)。这样做是首选,因为你不必这么做(相对昂贵)的转换。你可以简单地在 long 时间戳值上进行基本操作。



使用SQLite的内置日期功能的其他几个选项(如此处所述),但最终如果您想要将其拉入Android / Java代码,并将其作为 Date 对象进行操作,您将不得不进行转换。


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?

解决方案

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.

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

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