Android DateTime比较具有Date Only精度 [英] Android DateTime comparison with Date Only precision

查看:79
本文介绍了Android DateTime比较具有Date Only精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了许多有关如何比较2个日期的参考,但是它们似乎都在比较DateTime而不是自己比较Dates。

I find a number of references on how to compare 2 dates, but they all seem to compare the DateTime rather than the Dates by themselves.

我将LastUpdate保存到SQLite中,这要求它是DateTime。
稍后我将以字符串的形式获取该值,然后将其转换回Android日期(实际上是一个DateTime)。

I am saving off a LastUpdate into SQLite which requires it to be a DateTime. At a later time I am retrieving that value as a String then converting it back to an Android Date (which is really a DateTime).

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date LastDate = formatter.parse(LastUpdate);  

然后将其与今天(也是DateTime)进行比较

Then I am comparing it to Today (which is also a DateTime)

Date today = Calendar.getInstance().getTime();  

我正尝试使用

today.compareTo(LastDate)

但似乎精度其中

表示即使日期相同但一个时间不同,基于时间部分的结果还是+或-。

But it seems as though the precision of that goes down to the Time part.
Meaning that even if the Dates are the same, but one time is different, the result is either + or - based on the Time part.

我只想以完整的Date精度工作,而忽略Time部分。

I only want to work with a precision of a full Date, ignoring the Time part.

如何获得这种精确度?

How can I get that level of precision?

谢谢

推荐答案

看看isToday(long when) nofollow> DateUtils.class 。它只是比较年,月和monthDay。如果不是您要的日期,可以将日期设置为午夜,然后再进行比较:

Take a look at isToday(long when) in the DateUtils.class. It just compares the year, month and the monthDay. If that's not what you are looking for you could set your dates to midnight before you compare them like:

    Calendar today = Calendar.getInstance();
    today.set(HOUR_OF_DAY, 0);
    today.set(MINUTE, 0);
    today.set(SECOND, 0);
    today.set(MILLISECOND, 0);

    Calendar date = Calendar.getInstance();
    date.setTime(formatter.parse(LastUpdate));
    date.set(HOUR_OF_DAY, 0);
    date.set(MINUTE, 0);
    date.set(SECOND, 0);
    date.set(MILLISECOND, 0);

    today.compareTo(date)

这篇关于Android DateTime比较具有Date Only精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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