简体   繁体   English

没有时间的日期比较

[英]Date compare without time

I realise this is a question asked many times, but I have sifted through and can't seem to find an answer that would assist me completely.我意识到这是一个被问过很多次的问题,但我已经筛选过了,似乎无法找到一个可以完全帮助我的答案。 I'd start with saying that I can't use Joda.我首先说我不能使用 Joda。

I am making a change to a program that requires me to compare a user input date (From Date) with a date from a column.我正在对一个程序进行更改,该程序要求我将用户输入日期(起始日期)与列中的日期进行比较。

The request can't be done using SQL as the filter for the date check on this column has to be ran after the results are returned.不能使用 SQL 完成请求,因为必须在返回结果后运行此列上的日期检查过滤器。

The code is checking an entry on the table, making a comparison between the "From Date" and the field entry, and then it will display that row of data dependent on whether the boolean returns a true or false.该代码正在检查表上的条目,在“起始日期”和字段条目之间进行比较,然后它将根据布尔值是返回真还是假来显示该行数据。

All the code is working fine apart from one section.除了一部分之外,所有代码都运行良好。 It checks if the date on the table is more than or equal to the "From Date".它检查表上的日期是否大于或等于“起始日期”。

Example:例子:

The user enters the date 13/04/2015.用户输入日期 13/04/2015。

The program SHOULD return all rows with the date field that is 13/04/2015 or later.程序应该返回日期字段为 13/04/2015 或之后的所有行。

The program actually returns all dates that are 1 day after this date.该程序实际上返回此日期之后 1 天的所有日期。 ie anything that is 14/04/2015 and onwards.即 14/04/2015 及以后的任何内容。

My code for the check on this section is:我在这部分检查的代码是:

private boolean checkArngdDateCriteria(OrderBean singleOrderBean) {
    if (getSearchArngdFromDate().getValue()!= null || getSearchArngdToDate().getValue()!= null) {
        Date checkArngdToDate = (Date)getSearchArngdToDate().getValue();
        Date checkArngdFromDate = (Date)getSearchArngdFromDate().getValue();

        if (getSearchArngdFromDate().getValue()!= null && getSearchArngdToDate().getValue()== null && singleOrderBean.getOrdStatD()!= null) {
            if (singleOrderBean.getOrdStatD().after(checkArngdFromDate) || singleOrderBean.getOrdStatD().equals(checkArngdFromDate)) {
                return true;
            }
        }

EDIT:编辑:

 //This retrieves the date from the results table singleOrderBean.getOrdStatD() //This retrieves the value from the user input field (retrieved as Object) getSearchArngdFromDate().getValue()

Does anybody know a bit of code that would make it so the From Date would be treated as equal or less than the table date if they were both 13/04/2015?有没有人知道一些代码,如果它们都是 13/04/2015,那么From Date将被视为等于或小于表日期?

The Date class includes time fields (and various other fields). Date 类包括时间字段(和各种其他字段)。 You are comparing for 'after' or 'equals'.您正在比较“之后”或“等于”。 However, 'equals' is not doing what you think it's doing.然而,'equals' 并没有做你认为它在做的事情。 It's not just comparing dates.这不仅仅是比较日期。 It will only return true if all fields are equal.如果所有字段都相等,它只会返回true The easiest way to fix it is, instead of doing 'after or equals', do 'not before'.解决它的最简单方法是,不要执行“之后或等于”,而是执行“不之前”。

        if (!singleOrderBean.getOrdStatD().before(checkArngdFromDate)) {
            return true;
        }

For anybody wondering, the final code ended up as:对于任何想知道的人,最终代码最终为:

private boolean checkArngdDateCriteria(OrderBean singleOrderBean) {
    if (getSearchArngdFromDate().getValue()!= null || getSearchArngdToDate().getValue()!= null) {
        Date checkArngdFromDate = (Date)getSearchArngdFromDate().getValue();
        Date checkArngdToDate = (Date)getSearchArngdToDate().getValue();

        if (getSearchArngdFromDate().getValue()!= null) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(checkArngdFromDate);
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            checkArngdFromDate = cal.getTime();
        }

        if (getSearchArngdFromDate().getValue()!= null && getSearchArngdToDate().getValue()== null && singleOrderBean.getOrdStatD()!= null) {
            if (!singleOrderBean.getOrdStatD().before(checkArngdFromDate)) {
                return true;
            }
        }

I had to put the Calendar section inside an if statement so that it wouldn't interfere with a later if statement that checks if (getSearchArngdFromDate().getValue()== null && ...)我不得不把日历部分内部if statement ,以便它不会与后面的干扰if statement是检查if (getSearchArngdFromDate().getValue()== null && ...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM