字符串日期时间的转换:安卓 [英] Conversion of String to DateTime : Android
问题描述
我有一个字段(命名为时间,但类型是text)在我的SQLite数据库。在该列目前的格式为: YYYY:MM:DD_HH:MM
(2011年:11:01_11:30)。现在我有这个列进行排序,所以我认为我必须做两件事情:
I have a field (named as time, but type is TEXT) in my Sqlite Database. In current format of that column is YYYY:MM:DD_HH:MM
(as 2011:11:01_11:30 ). Now I have to sort this column, so I think I have to do two things:
- 在转换字符串值是最新的。
- 然后用排序列。
那么,我怎么可以转换成列的日期时间?或者有没有其他的办法做到这一点?
Then how can I convert that column into DateTime? Or is there any other way to do this?
推荐答案
要转换成字符串日期,在我的情况下,这个工作正常,
To convert String into Date, In my case this works fine,
String startTime = "2011-09-05 15:00:23";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date1 = dateFormat.parse(startTime);
编辑:
有关日期和时间的SQLite
For Date and Time in SQLite
1.2日期和时间数据类型
SQLite没有一个存储类预留的存储日期和/或时间。相反,内置日期和时间函数的SQLite能够存储日期和时间为TEXT,REAL或INTEGER的值:
SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:
* TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
* REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.
* INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.
应用程序可以选择存储日期和时间的任何格式,自由使用的格式之间进行转换的内置日期和时间功能。
Applications can chose to store dates and times in any of these formats and freely convert between formats using the built-in date and time functions.
所以,仅仅使用一个INTEGER或TEXT和使用ORDER BY子句进行排序。
这篇关于字符串日期时间的转换:安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!