commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Niall Pemberton <niall.pember...@gmail.com>
Subject Re: [Lang] Null safe date comparisson
Date Thu, 06 Jan 2011 18:27:34 GMT
On Thu, Jan 6, 2011 at 4:38 PM, sebb <sebbaz@gmail.com> wrote:
> On 6 January 2011 16:31, Ludwig Magnusson <ludwig@itcatapult.com> wrote:
>> Hi!
>>
>> I'm looking for a way to do null safe date comparison. Something like:
>>
>>
>>
>> public static boolean isBefore(Date first, Date second);
>>
>>
>>
>> The method would handle null in an appropriate way.
>
> What do you mean by appropriate?
>
> Does null mean beginning of time, or end of time, or now, or Y2K or
> something else?
>
> How does one choose the default?

I was thinking it would be usefule to have a null safe compare method
to ObjectUtils - something like

    public static int compare(Comparable c1, Comparable c2) {
        return compare(c1, c2, false);
    }
    public static int compare(Comparable c1, Comparable c2, boolean
nullMoreThan) {
        int result = 0;
        if ((c1 == null) || (c2 == null)) {
            if (nullMoreThan) {
                result = (c1 == null ? 1 : 0) - (c2 == null ? 1 : 0);
            } else {
                result = (c1 == null ? -1 : 0) - (c2 == null ? -1 : 0);
            }
        } else {
            result = c1.compareTo(c2);
        }
        return result;
    }

As dates implement comparable it would work for them as well

Niall

>> I haven't found any method like that in DateUtils.
>>
>> Is there one somewhere?
>>
>> /Ludwig
>>
>>
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message