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 |
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 |
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 <[hidden email]> 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 > > > > 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 > > |
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); } |
Administrator
|
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" <[hidden email]> To: <[hidden email]> 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); > } |
Ok, I'll write up a ticket in JIRA and submit a patch.
|
In reply to this post by Jacques Le Roux
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" <[hidden email]> > To: <[hidden email]> > 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); >> } |
Administrator
|
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" <[hidden email]> > > To: <[hidden email]> > > 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); > >> } > |
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 |
In reply to this post by Jacques Le Roux
Ok, I'll write up a ticket in JIRA and submit a patch.
|
In reply to this post by David E Jones-2
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" <[hidden email]> > > To: <[hidden email]> > > 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); > >> } > |
In reply to this post by Jacques Le Roux
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 |
In reply to this post by Jacques Le Roux
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" <[hidden email]> > To: <[hidden email]> > 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); >> } |
In reply to this post by John Martin
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); > } > |
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); >> } >> > > |
Administrator
|
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 |
Free forum by Nabble | Edit this page |