You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ofbiz.apache.org by John Martin <pb...@gmail.com> on 2006/10/06 15:22:44 UTC

Date Comparison Function

Hi All,

I searched through the code base looking for a simple date comparison
method to compare two dates excluding time.  I was unable to find one.
 Is there one that I can use?

I want the ability to compare data A to B and determine if it is <, =,
or >.  I've writen one, compareDates(Date A, Date B) compares the day,
month and year of two dates, returing 0, -1 or +1 indicating whether
the first date is equal, before or after the second.

If there isn't one, I propose adding my method to the
framework\base\src\base\org\ofbiz\base\util\UtilDateTime.java class.

There is a method in commons validation jar but this jar is not
included in the distro.
http://jakarta.apache.org/commons/validator/apidocs/org/apache/commons/validator/routines/DateValidator.html

Should we include my method or the commons validator jar?

Thanks,

John

Re: Date Comparison Function

Posted by Jacques Le Roux <ja...@les7arts.com>.
Hi all,

Something weird happenned here (in my mail box). Have you also received this complete resurrected thread dating from last year ?
http://www.nabble.com/Date-Comparison-Function-tf2395467.html#a6687467

Thanks

Jacques

Re: Date Comparison Function

Posted by Adrian Crum <ad...@hlmksw.com>.
Thinking about this more, you should use the UtilDateTime.toCalendar(...) method to construct two 
Calendar objects, then use them for the comparison.

Helpful link:

http://www.icu-project.org/docs/papers/international_calendars_in_java.html


Adrian Crum wrote:

> Why not compare the milliseconds value?
> 
> 
> John Martin wrote:
> 
>> The problem with that is that comparing datetime means that 10/06/2006
>> 12:00:01 != 10/06/2006 18:30:00.  I just want to see if a Date is the
>> same, before, or after another.  The time messes up the tests.
>>
>> I think using the commons/validator is the right thing to do since the
>> less code we write, the less we have to maintain.  On the otherhand,
>> my method is short and sweet.
>>
>>    /**
>>     * compares two dates (excluding time) to determine if date A is
>> equal, before or after date B
>>     * @param dateA     a java.util.Date object
>>     * @param dateB     a java.util.Date object
>>     * @return  int     a value of zero (0) indicates the dates are
>> equal; a value less than zero (< 0) date A is prior to B; and a
>>     *                  value greater than zero (> 0) indicates date A
>> occurs after B
>>     */
>>    public static int compareDates(java.util.Date dateA, java.util.Date 
>> dateB) {
>>        String strDateA = UtilDateTime.toDateString(dateA, "yyyyMMdd");
>>        String strDateB = UtilDateTime.toDateString(dateB, "yyyyMMdd");
>>        return strDateA.compareTo(strDateB);
>>    }
>>
> 
> 


Re: Date Comparison Function

Posted by Adrian Crum <ad...@hlmksw.com>.
Why not compare the milliseconds value?


John Martin wrote:

> The problem with that is that comparing datetime means that 10/06/2006
> 12:00:01 != 10/06/2006 18:30:00.  I just want to see if a Date is the
> same, before, or after another.  The time messes up the tests.
> 
> I think using the commons/validator is the right thing to do since the
> less code we write, the less we have to maintain.  On the otherhand,
> my method is short and sweet.
> 
>    /**
>     * compares two dates (excluding time) to determine if date A is
> equal, before or after date B
>     * @param dateA     a java.util.Date object
>     * @param dateB     a java.util.Date object
>     * @return  int     a value of zero (0) indicates the dates are
> equal; a value less than zero (< 0) date A is prior to B; and a
>     *                  value greater than zero (> 0) indicates date A
> occurs after B
>     */
>    public static int compareDates(java.util.Date dateA, java.util.Date 
> dateB) {
>        String strDateA = UtilDateTime.toDateString(dateA, "yyyyMMdd");
>        String strDateB = UtilDateTime.toDateString(dateB, "yyyyMMdd");
>        return strDateA.compareTo(strDateB);
>    }
> 


Re: Date Comparison Function

Posted by John Martin <pb...@gmail.com>.
Ok, I'll write up a ticket in JIRA and submit a patch.

Re: Date Comparison Function

Posted by John Martin <fa...@gmail.com>.
Ok, I'll write up a ticket in JIRA and submit a patch.


Re: Date Comparison Function

Posted by Jacques Le Roux <fa...@gmail.com>.
John,

As you may have seen David and Jacopo prefer to use already jakarta commons
libraries.
That makes sense of course : less code to maintain, OFBiz is already pretty
huge :o)

How might we organize that task ?

Jacques


> 
> My preference from a code management perspective would be to not have  
> any code in OFBiz that we could include a library for, especially the  
> jakarta commons libraries as sooner or later we'll probably have all  
> of them in OFBiz anyway... ;)
> 
> -David
> 
> 
> On Oct 6, 2006, at 7:24 PM, Jacques Le Roux wrote:
> 
> > Hi John, Jacopo and Chris,
> >
> > Perhaps before embedding the whole Apache's validator we may simply  
> > add the below method as suggested John ?
> >
> > If later we need more validations or other stuffes we migh include  
> > Apache's tools (validator, math, etc.)
> >
> > Jacques
> >
> > ----- Original Message -----
> > From: "John Martin" <pb...@gmail.com>
> > To: <of...@incubator.apache.org>
> > Sent: Friday, October 06, 2006 7:14 PM
> > Subject: Re: Date Comparison Function
> >
> >
> >> The problem with that is that comparing datetime means that  
> >> 10/06/2006
> >> 12:00:01 != 10/06/2006 18:30:00.  I just want to see if a Date is the
> >> same, before, or after another.  The time messes up the tests.
> >>
> >> I think using the commons/validator is the right thing to do since  
> >> the
> >> less code we write, the less we have to maintain.  On the otherhand,
> >> my method is short and sweet.
> >>
> >>     /**
> >>      * compares two dates (excluding time) to determine if date A is
> >> equal, before or after date B
> >>      * @param dateA     a java.util.Date object
> >>      * @param dateB     a java.util.Date object
> >>      * @return  int     a value of zero (0) indicates the dates are
> >> equal; a value less than zero (< 0) date A is prior to B; and a
> >>      *                  value greater than zero (> 0) indicates  
> >> date A
> >> occurs after B
> >>      */
> >>     public static int compareDates(java.util.Date dateA,  
> >> java.util.Date dateB) {
> >>         String strDateA = UtilDateTime.toDateString(dateA,  
> >> "yyyyMMdd");
> >>         String strDateB = UtilDateTime.toDateString(dateB,  
> >> "yyyyMMdd");
> >>         return strDateA.compareTo(strDateB);
> >>     }
>


Re: Date Comparison Function

Posted by John Martin <fa...@gmail.com>.
Well, I tend to agree and think that the commons jar make the most
sense.  I'll write up the JIRA and add a link to the libs.  I will
need this library in place for some code that I'm implementing for the
DHL enhancements.

Thanks for the feedback!

John


Re: Date Comparison Function

Posted by John Martin <pb...@gmail.com>.
Well, I tend to agree and think that the commons jar make the most
sense.  I'll write up the JIRA and add a link to the libs.  I will
need this library in place for some code that I'm implementing for the
DHL enhancements.

Thanks for the feedback!

John

Re: Date Comparison Function

Posted by Jacques Le Roux <ja...@les7arts.com>.
John,

As you may have seen David and Jacopo prefer to use already jakarta commons libraries.
That makes sense of course : less code to maintain, OFBiz is already pretty huge :o)

How might we organize that task ?

Jacques


> 
> My preference from a code management perspective would be to not have  
> any code in OFBiz that we could include a library for, especially the  
> jakarta commons libraries as sooner or later we'll probably have all  
> of them in OFBiz anyway... ;)
> 
> -David
> 
> 
> On Oct 6, 2006, at 7:24 PM, Jacques Le Roux wrote:
> 
> > Hi John, Jacopo and Chris,
> >
> > Perhaps before embedding the whole Apache's validator we may simply  
> > add the below method as suggested John ?
> >
> > If later we need more validations or other stuffes we migh include  
> > Apache's tools (validator, math, etc.)
> >
> > Jacques
> >
> > ----- Original Message -----
> > From: "John Martin" <pb...@gmail.com>
> > To: <of...@incubator.apache.org>
> > Sent: Friday, October 06, 2006 7:14 PM
> > Subject: Re: Date Comparison Function
> >
> >
> >> The problem with that is that comparing datetime means that  
> >> 10/06/2006
> >> 12:00:01 != 10/06/2006 18:30:00.  I just want to see if a Date is the
> >> same, before, or after another.  The time messes up the tests.
> >>
> >> I think using the commons/validator is the right thing to do since  
> >> the
> >> less code we write, the less we have to maintain.  On the otherhand,
> >> my method is short and sweet.
> >>
> >>     /**
> >>      * compares two dates (excluding time) to determine if date A is
> >> equal, before or after date B
> >>      * @param dateA     a java.util.Date object
> >>      * @param dateB     a java.util.Date object
> >>      * @return  int     a value of zero (0) indicates the dates are
> >> equal; a value less than zero (< 0) date A is prior to B; and a
> >>      *                  value greater than zero (> 0) indicates  
> >> date A
> >> occurs after B
> >>      */
> >>     public static int compareDates(java.util.Date dateA,  
> >> java.util.Date dateB) {
> >>         String strDateA = UtilDateTime.toDateString(dateA,  
> >> "yyyyMMdd");
> >>         String strDateB = UtilDateTime.toDateString(dateB,  
> >> "yyyyMMdd");
> >>         return strDateA.compareTo(strDateB);
> >>     }
>

Re: Date Comparison Function

Posted by David E Jones <jo...@undersunconsulting.com>.
My preference from a code management perspective would be to not have  
any code in OFBiz that we could include a library for, especially the  
jakarta commons libraries as sooner or later we'll probably have all  
of them in OFBiz anyway... ;)

-David


On Oct 6, 2006, at 7:24 PM, Jacques Le Roux wrote:

> Hi John, Jacopo and Chris,
>
> Perhaps before embedding the whole Apache's validator we may simply  
> add the below method as suggested John ?
>
> If later we need more validations or other stuffes we migh include  
> Apache's tools (validator, math, etc.)
>
> Jacques
>
> ----- Original Message -----
> From: "John Martin" <pb...@gmail.com>
> To: <of...@incubator.apache.org>
> Sent: Friday, October 06, 2006 7:14 PM
> Subject: Re: Date Comparison Function
>
>
>> The problem with that is that comparing datetime means that  
>> 10/06/2006
>> 12:00:01 != 10/06/2006 18:30:00.  I just want to see if a Date is the
>> same, before, or after another.  The time messes up the tests.
>>
>> I think using the commons/validator is the right thing to do since  
>> the
>> less code we write, the less we have to maintain.  On the otherhand,
>> my method is short and sweet.
>>
>>     /**
>>      * compares two dates (excluding time) to determine if date A is
>> equal, before or after date B
>>      * @param dateA     a java.util.Date object
>>      * @param dateB     a java.util.Date object
>>      * @return  int     a value of zero (0) indicates the dates are
>> equal; a value less than zero (< 0) date A is prior to B; and a
>>      *                  value greater than zero (> 0) indicates  
>> date A
>> occurs after B
>>      */
>>     public static int compareDates(java.util.Date dateA,  
>> java.util.Date dateB) {
>>         String strDateA = UtilDateTime.toDateString(dateA,  
>> "yyyyMMdd");
>>         String strDateB = UtilDateTime.toDateString(dateB,  
>> "yyyyMMdd");
>>         return strDateA.compareTo(strDateB);
>>     }



Re: Date Comparison Function

Posted by David E Jones <fa...@gmail.com>.
My preference from a code management perspective would be to not have  
any code in OFBiz that we could include a library for, especially the  
jakarta commons libraries as sooner or later we'll probably have all  
of them in OFBiz anyway... ;)

-David


On Oct 6, 2006, at 7:24 PM, Jacques Le Roux wrote:

> Hi John, Jacopo and Chris,
>
> Perhaps before embedding the whole Apache's validator we may simply  
> add the below method as suggested John ?
>
> If later we need more validations or other stuffes we migh include  
> Apache's tools (validator, math, etc.)
>
> Jacques
>
> ----- Original Message -----
> From: "John Martin" <pb...@gmail.com>
> To: <of...@incubator.apache.org>
> Sent: Friday, October 06, 2006 7:14 PM
> Subject: Re: Date Comparison Function
>
>
>> The problem with that is that comparing datetime means that  
>> 10/06/2006
>> 12:00:01 != 10/06/2006 18:30:00.  I just want to see if a Date is the
>> same, before, or after another.  The time messes up the tests.
>>
>> I think using the commons/validator is the right thing to do since  
>> the
>> less code we write, the less we have to maintain.  On the otherhand,
>> my method is short and sweet.
>>
>>     /**
>>      * compares two dates (excluding time) to determine if date A is
>> equal, before or after date B
>>      * @param dateA     a java.util.Date object
>>      * @param dateB     a java.util.Date object
>>      * @return  int     a value of zero (0) indicates the dates are
>> equal; a value less than zero (< 0) date A is prior to B; and a
>>      *                  value greater than zero (> 0) indicates  
>> date A
>> occurs after B
>>      */
>>     public static int compareDates(java.util.Date dateA,  
>> java.util.Date dateB) {
>>         String strDateA = UtilDateTime.toDateString(dateA,  
>> "yyyyMMdd");
>>         String strDateB = UtilDateTime.toDateString(dateB,  
>> "yyyyMMdd");
>>         return strDateA.compareTo(strDateB);
>>     }



Re: Date Comparison Function

Posted by Jacques Le Roux <ja...@les7arts.com>.
Hi John, Jacopo and Chris,

Perhaps before embedding the whole Apache's validator we may simply add the below method as suggested John ?

If later we need more validations or other stuffes we migh include Apache's tools (validator, math, etc.)

Jacques

----- Original Message ----- 
From: "John Martin" <pb...@gmail.com>
To: <of...@incubator.apache.org>
Sent: Friday, October 06, 2006 7:14 PM
Subject: Re: Date Comparison Function


> The problem with that is that comparing datetime means that 10/06/2006
> 12:00:01 != 10/06/2006 18:30:00.  I just want to see if a Date is the
> same, before, or after another.  The time messes up the tests.
> 
> I think using the commons/validator is the right thing to do since the
> less code we write, the less we have to maintain.  On the otherhand,
> my method is short and sweet.
> 
>     /**
>      * compares two dates (excluding time) to determine if date A is
> equal, before or after date B
>      * @param dateA     a java.util.Date object
>      * @param dateB     a java.util.Date object
>      * @return  int     a value of zero (0) indicates the dates are
> equal; a value less than zero (< 0) date A is prior to B; and a
>      *                  value greater than zero (> 0) indicates date A
> occurs after B
>      */
>     public static int compareDates(java.util.Date dateA, java.util.Date dateB) {
>         String strDateA = UtilDateTime.toDateString(dateA, "yyyyMMdd");
>         String strDateB = UtilDateTime.toDateString(dateB, "yyyyMMdd");
>         return strDateA.compareTo(strDateB);
>     }

Re: Date Comparison Function

Posted by John Martin <pb...@gmail.com>.
The problem with that is that comparing datetime means that 10/06/2006
12:00:01 != 10/06/2006 18:30:00.  I just want to see if a Date is the
same, before, or after another.  The time messes up the tests.

I think using the commons/validator is the right thing to do since the
less code we write, the less we have to maintain.  On the otherhand,
my method is short and sweet.

    /**
     * compares two dates (excluding time) to determine if date A is
equal, before or after date B
     * @param dateA     a java.util.Date object
     * @param dateB     a java.util.Date object
     * @return  int     a value of zero (0) indicates the dates are
equal; a value less than zero (< 0) date A is prior to B; and a
     *                  value greater than zero (> 0) indicates date A
occurs after B
     */
    public static int compareDates(java.util.Date dateA, java.util.Date dateB) {
        String strDateA = UtilDateTime.toDateString(dateA, "yyyyMMdd");
        String strDateB = UtilDateTime.toDateString(dateB, "yyyyMMdd");
        return strDateA.compareTo(strDateB);
    }

Re: Date Comparison Function

Posted by Chris Howe <cj...@yahoo.com>.
You should be able to subtract one datetime from
another datetime.  If a-b>0 then a is after than b, if
a-b<0, then b is after a.  if a=b they are the same. 
i know there are at least a couple of examples of this
in bsh files throughout the code base

--- Jacopo Cappellato <ti...@sastau.it> wrote:

> Hi John,
> 
> I don't remember if we have something similar in
> OFBiz, but if not I 
> think it is a good idea to include it; maybe I'd
> prefer to include the 
> commons validator jar (and also the commons math jar
> 
> http://jakarta.apache.org/commons/math/ would be
> interesting for reports 
> etc... but this is anoether subject).
> 
> In the meantime, thanks for your ideas and research,
> 
> Jacopo
> 
> John Martin wrote:
> > Hi All,
> > 
> > I searched through the code base looking for a
> simple date comparison
> > method to compare two dates excluding time.  I was
> unable to find one.
> > Is there one that I can use?
> > 
> > I want the ability to compare data A to B and
> determine if it is <, =,
> > or >.  I've writen one, compareDates(Date A, Date
> B) compares the day,
> > month and year of two dates, returing 0, -1 or +1
> indicating whether
> > the first date is equal, before or after the
> second.
> > 
> > If there isn't one, I propose adding my method to
> the
> >
>
framework\base\src\base\org\ofbiz\base\util\UtilDateTime.java
> class.
> > 
> > There is a method in commons validation jar but
> this jar is not
> > included in the distro.
> >
>
http://jakarta.apache.org/commons/validator/apidocs/org/apache/commons/validator/routines/DateValidator.html
> 
> > 
> > 
> > Should we include my method or the commons
> validator jar?
> > 
> > Thanks,
> > 
> > John
> 
> 


Re: Date Comparison Function

Posted by Jacopo Cappellato <ti...@sastau.it>.
Hi John,

I don't remember if we have something similar in OFBiz, but if not I 
think it is a good idea to include it; maybe I'd prefer to include the 
commons validator jar (and also the commons math jar 
http://jakarta.apache.org/commons/math/ would be interesting for reports 
etc... but this is anoether subject).

In the meantime, thanks for your ideas and research,

Jacopo

John Martin wrote:
> Hi All,
> 
> I searched through the code base looking for a simple date comparison
> method to compare two dates excluding time.  I was unable to find one.
> Is there one that I can use?
> 
> I want the ability to compare data A to B and determine if it is <, =,
> or >.  I've writen one, compareDates(Date A, Date B) compares the day,
> month and year of two dates, returing 0, -1 or +1 indicating whether
> the first date is equal, before or after the second.
> 
> If there isn't one, I propose adding my method to the
> framework\base\src\base\org\ofbiz\base\util\UtilDateTime.java class.
> 
> There is a method in commons validation jar but this jar is not
> included in the distro.
> http://jakarta.apache.org/commons/validator/apidocs/org/apache/commons/validator/routines/DateValidator.html 
> 
> 
> Should we include my method or the commons validator jar?
> 
> Thanks,
> 
> John