【发布时间】:2012-11-26 07:56:16
【问题描述】:
我有 data+time 在 毫秒 内保存在数据库 (sq lite) 中,现在我想从特定日期的 sq-lite 获取数据,我日期格式为“26-December-2012”,如何将其与毫秒进行比较。
从数据库中获取数据的查询应该是什么?
【问题讨论】:
标签: android datetime android-sqlite milliseconds android-date
我有 data+time 在 毫秒 内保存在数据库 (sq lite) 中,现在我想从特定日期的 sq-lite 获取数据,我日期格式为“26-December-2012”,如何将其与毫秒进行比较。
从数据库中获取数据的查询应该是什么?
【问题讨论】:
标签: android datetime android-sqlite milliseconds android-date
只需使用数据库中的 timeInMilliseconds 数据创建一个新的Calendar。
因此,如果您有时间在名为 date 的列中,并且数据位于名为 myTable 的表中,则查询得到的是:
select date from myTable ... other constraints
在android中,只需使用从数据库中检索到的long值来构造一个新的Calendar:
Calendar cal = new Calendar.getInstance();
cal.setTimeInMillis(timeInMsFromDatabase);
一旦有了Calendar 对象,就可以使用get(int field) method 检索所需的值。
或者,您可以使用DateFormat 类。
有意义吗?
【讨论】:
您必须将毫秒转换为日期格式,然后比较两个日期
转换成日期格式
public static String getDate(long milliSeconds, String dateFormat)
{`enter code here`
// Create a DateFormatter object for displaying date in specified format.
DateFormat formatter = new SimpleDateFormat(dateFormat);
// Create a calendar object that will convert the date and time value in milliseconds to date.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
return formatter.format(calendar.getTime());
}
比较日期
SimpleDateFormat curFormater = new SimpleDateFormat("dd/MM/yyyy");
Date date1 = curFormater.parse(date1Str);
Date date2 = curFormater.parse(date2Str);
if (date1.before(date2))
{
}
【讨论】:
希望对你有帮助
public long getDateLong(String dateString, String format) throws ParseException
{
SimpleDateFormat f = new SimpleDateFormat(format);
Date d = f.parse(dateString);
return d.getTime();
}
//
long timeMillis; // Your long time millis
boolean compare = timeMillis > getDateLong("26-December-2012", "dd-MMMM-yyyy");
【讨论】: