java - Weird behaviour of compareTo(GregorianCalendar c) -


could tell me why fillowing code:

int = new gregoriancalendar(2015,3,31,7,45).compareto(         new gregoriancalendar(2015,4,1,7,45); system.out.println(a); 

prints out 0?

is there way make work right?

ps: need sort strings out date , use comparator:

array.sort(new comparator<string>() {     @override     public int compare(string o1, string o2) {         gregoriancalendar cal1 = new gregoriancalendar(integer.parseint(o1.replaceall(p, "$7")),                 integer.parseint(o1.replaceall(p, "$6")), integer.parseint(o1.replaceall(p, "$5")),                 integer.parseint(o1.replaceall(p, "$8")), integer.parseint(o1.replaceall(p, "$9")));         gregoriancalendar cal2 = new gregoriancalendar(integer.parseint(o2.replaceall(p, "$7")),                 integer.parseint(o2.replaceall(p, "$6")), integer.parseint(o2.replaceall(p, "$5")),                 integer.parseint(o2.replaceall(p, "$8")), integer.parseint(o2.replaceall(p, "$9")));         return cal1.compareto(cal2);     } }); 

it uses regular expressions, sorted correctly , dates provided not sorted right.

you're comparing "april 31st" may 1st. there is no april 31st, it's rolling on may 1st anyway. (okay, make more sense throw exception, hey... that's far worst piece of api design in calendar.)

i recommend using simpledateformat parse string representations of date/time values, instead of doing yourself. aside else, simpledateformat "knows" months 0-based in java... basic bug you've fallen foul of. code lot more readable, suspect.

do need keep collection collection of strings anyway? if they're just dates, convert collection of date type (ideally using joda time or java 8's java.time package). if they're log entries have date other information, convert them representation of first. either way, you've got collection more naturally represents information holds.


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -