将字符串转换为DateTime:Android [英] Conversion of String to DateTime : Android

查看:179
本文介绍了将字符串转换为DateTime:Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Sqlite数据库中有一个字段(以时间命名,但是类型为TEXT)。该列的当前格式为 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:


  1. 将字符串值转换为日期

  2. 然后用列排序。

然后如何将该列转换为DateTime?或者还有其他方法可以做到这一点吗?

Then how can I convert that column into DateTime? Or is there any other way to do this?

推荐答案

将String转换为Date,在我的情况下, p>

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子句进行排序。

这篇关于将字符串转换为DateTime:Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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