You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Ludwig Magnusson <lu...@itcatapult.com> on 2011/01/06 17:31:31 UTC

[Lang] Null safe date comparisson

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.

I haven't found any method like that in DateUtils.

Is there one somewhere? 

/Ludwig

 


RE: [Lang] Null safe date comparisson

Posted by Gary Gregory <GG...@seagullsoftware.com>.
> -----Original Message-----
> From: Niall Pemberton [mailto:niall.pemberton@gmail.com]
> Sent: Thursday, January 06, 2011 13:52
> To: Commons Users List
> Subject: Re: [Lang] Null safe date comparisson
> 
> On Thu, Jan 6, 2011 at 6:47 PM, Gary Gregory
> <GG...@seagullsoftware.com> wrote:
> >> -----Original Message-----
> >> From: Niall Pemberton [mailto:niall.pemberton@gmail.com]
> >> Sent: Thursday, January 06, 2011 13:28
> >> To: Commons Users List
> >> Subject: Re: [Lang] Null safe date comparisson
> >>
> >> On Thu, Jan 6, 2011 at 4:38 PM, sebb <se...@gmail.com> wrote:
> >> > On 6 January 2011 16:31, Ludwig Magnusson <lu...@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
> >
> > +1 That would be useful. Same for equals?
> 
> Its there already!
> 
> http://tinyurl.com/36e6oes

Doh!
Gary

> 
> Niall
> 
> > Gary
> >
> >>
> >> >> 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


Re: [Lang] Null safe date comparisson

Posted by Niall Pemberton <ni...@gmail.com>.
On Thu, Jan 6, 2011 at 6:47 PM, Gary Gregory
<GG...@seagullsoftware.com> wrote:
>> -----Original Message-----
>> From: Niall Pemberton [mailto:niall.pemberton@gmail.com]
>> Sent: Thursday, January 06, 2011 13:28
>> To: Commons Users List
>> Subject: Re: [Lang] Null safe date comparisson
>>
>> On Thu, Jan 6, 2011 at 4:38 PM, sebb <se...@gmail.com> wrote:
>> > On 6 January 2011 16:31, Ludwig Magnusson <lu...@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
>
> +1 That would be useful. Same for equals?

Its there already!

http://tinyurl.com/36e6oes

Niall

> Gary
>
>>
>> >> 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


RE: [Lang] Null safe date comparisson

Posted by Gary Gregory <GG...@seagullsoftware.com>.
> -----Original Message-----
> From: Niall Pemberton [mailto:niall.pemberton@gmail.com]
> Sent: Thursday, January 06, 2011 13:28
> To: Commons Users List
> Subject: Re: [Lang] Null safe date comparisson
> 
> On Thu, Jan 6, 2011 at 4:38 PM, sebb <se...@gmail.com> wrote:
> > On 6 January 2011 16:31, Ludwig Magnusson <lu...@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

+1 That would be useful. Same for equals?

Gary

> 
> >> 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


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


Re: [Lang] Null safe date comparisson

Posted by Niall Pemberton <ni...@gmail.com>.
On Thu, Jan 6, 2011 at 6:27 PM, Niall Pemberton
<ni...@gmail.com> wrote:
> On Thu, Jan 6, 2011 at 4:38 PM, sebb <se...@gmail.com> wrote:
>> On 6 January 2011 16:31, Ludwig Magnusson <lu...@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

I've added this:

https://issues.apache.org/jira/browse/LANG-667
http://svn.apache.org/viewvc?view=revision&revision=1056124

> 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


Re: [Lang] Null safe date comparisson

Posted by Niall Pemberton <ni...@gmail.com>.
On Thu, Jan 6, 2011 at 4:38 PM, sebb <se...@gmail.com> wrote:
> On 6 January 2011 16:31, Ludwig Magnusson <lu...@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


RE: [Lang] Null safe date comparisson

Posted by Ludwig Magnusson <lu...@itcatapult.com>.
By appropriate I mean that I don't really have a specification for the
behavior. All I need is a method that can handle null without throwing an
exception. If such methods were to exist, the creators would surely have
thought of different ways to solve null arguments and they would probably
have solved it in some way if the code standard was good (and apaches
usually is).

 

My question was. Does such a method/library exist to anyone's knowledge?

If not, perhaps we could build one if people are interested and then we
could discuss what behavior is "appropriate".

 

/Ludwig

 

From: sebb [mailto:sebbaz@gmail.com] 
Sent: den 6 januari 2011 17:39
To: Commons Users List
Subject: Re: [Lang] Null safe date comparisson

 

On 6 January 2011 16:31, Ludwig Magnusson <lu...@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 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

  _____  

No virus found in this message.
Checked by AVG - www.avg.com
Version: 10.0.1191 / Virus Database: 1435/3362 - Release Date: 01/05/11


Re: [Lang] Null safe date comparisson

Posted by sebb <se...@gmail.com>.
On 6 January 2011 16:31, Ludwig Magnusson <lu...@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 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


RE: [Lang] Null safe date comparisson

Posted by Ludwig Magnusson <lu...@itcatapult.com>.
I use Joda-time a lot so I'm familiar with it. Do they really have a class
that does what I asked?

As far as I know, they only have their own implementations. I need null-safe
comparisons on java.util.Date

/Ludwig

 

From: Gary Gregory [mailto:GGregory@seagullsoftware.com] 
Sent: den 6 januari 2011 17:38
To: Commons Users List
Subject: RE: [Lang] Null safe date comparisson

 

Look at Joda-Time http://joda-time.sourceforge.net/

Gary Gregory
Senior Software Engineer
Rocket Software
3340 Peachtree Road, Suite 820 . Atlanta, GA 30326 . USA
Tel: +1.404.760.1560
Email: ggregory@seagullsoftware.com
Web: seagull.rocketsoftware.com 


> -----Original Message-----
> From: Ludwig Magnusson [mailto:ludwig@itcatapult.com]
> Sent: Thursday, January 06, 2011 11:32
> To: user@commons.apache.org
> Subject: [Lang] Null safe date comparisson
>
> 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.
>
> 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

  _____  

No virus found in this message.
Checked by AVG - www.avg.com
Version: 10.0.1191 / Virus Database: 1435/3362 - Release Date: 01/05/11


RE: [Lang] Null safe date comparisson

Posted by Gary Gregory <GG...@seagullsoftware.com>.
Look at Joda-Time http://joda-time.sourceforge.net/

Gary Gregory
Senior Software Engineer
Rocket Software
3340 Peachtree Road, Suite 820 . Atlanta, GA 30326 . USA
Tel: +1.404.760.1560
Email: ggregory@seagullsoftware.com
Web: seagull.rocketsoftware.com  


> -----Original Message-----
> From: Ludwig Magnusson [mailto:ludwig@itcatapult.com]
> Sent: Thursday, January 06, 2011 11:32
> To: user@commons.apache.org
> Subject: [Lang] Null safe date comparisson
> 
> 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.
> 
> 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