Java - 将日期格式化的字符串从SQL数据库转换为日期对象进行比较 [英] Java - convert date formatted String from SQL db to date object for comparison

查看:179
本文介绍了Java - 将日期格式化的字符串从SQL数据库转换为日期对象进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串存储在数据库中(例如: Sat Jul 09 14:20:31 EDT 2011

I have a string stored in a database (example: Sat Jul 09 14:20:31 EDT 2011)

这是存储为字符串(我知道SQL的日期功能,但我现在不在探索)。

This is stored as string (I am aware of SQLs date capabilities, but I'm not exploring that at this point).

我想拉这个强大并使用 Date.compare 函数

I want to pull this strong and compare it with the current time using the Date.compare function

$ b $与当前时间进行比较b Date currentDate = new Date(); //新的当前时间值
mCursor.getString(mCursor.getColumnIndex(TIMECOLUMN)
//从SQL数据库拉出的旧存储时间值

Date currentDate = new Date(); // the new current time value mCursor.getString(mCursor.getColumnIndex(TIMECOLUMN) //the old stored time value, pulled from the SQL database

从数据库中我有一个已经格式化为正确日期的字符串值,但是我没有明显的功能来将其与当前日期作为日期对象进行比较。

From the database I have a string value already formatted as a proper date. But I don't have an obvious function to compare this to my current date as a date object.

我需要知道一个是否大于,小于或等于另一个,Date 比较函数将允许这个,但是对象需要是一个日期对象。

I need to know if one is greater than, less than, or equal to, the other. The Date compare function will allow this, but the object needs to be a date object.

我已尝试使用字符串日期格式播放的 SimpleDateFormat 一个解析例外的可解析日期,可能是因为它已经格式化了?

I have tried SimpleDateFormat seeded with the string's date format, but I get a parse exception "unparsable date", perhaps because it is already formatted?


Date currentDate = new Date();

SimpleDateFormat dateTest = new SimpleDateFormat(EM d HH:mm:ss zy);
if(currentDate.compareTo(dateTest.parse((mCursor.getString(mCursor.getColumnIndex(TIMECOLUMN))) ))> 0)

ret urns java.text.ParseException:不可稀释的日期:Sat Jul 09 14:20:31 EDT 2011

returns java.text.ParseException: Unparseable date: Sat Jul 09 14:20:31 EDT 2011

我怀疑我的SimpleDateFormat种子不正确。 Insight的评价

I suspect my SimpleDateFormat seed is incorrect. Insight Appreciated

推荐答案

我相信你只需要将格式字符串中的M更改为MMM:

I believe you just need to change the "M" to "MMM" in your format string:

SimpleDateFormat format = new SimpleDateFormat("E MMM d HH:mm:ss z y");

M是潜在的单位数值,例如1,10等。MMM是一个短的月份名称(例如Jul,Sep)。

"M" is a potentially-single-digit value, e.g. 1, 10 etc. "MMM" is a short month name (e.g. "Jul", "Sep").

但是, em>指定日期格式的区域设置。我也很强烈地鼓励你打破你目前的巨大的声明,使其更容易理解 debug:

However, you should probably also specify the locale of the date format. I'd also strongly encourage you to break your currently huge statement into several bits to make it easier to understand and debug:

int timeColumn = mCursor.getColumnIndex(TIMECOLUMN);
String dbDateText = mCursor.getString(timeColumn);
Date dbDate = format.parse(dbDateText);

if (currentDate.compareTo(dbDate) > 0) {
}

我还鼓励您使用 Joda时间进行日期/时间相关工作,并避免将日期/时间数据作为文本存储在数据库中:)

I'd also encourage you to use Joda Time for date/time-related work, and to avoid storing date/time data as text in the database :)

这篇关于Java - 将日期格式化的字符串从SQL数据库转换为日期对象进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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