Hi, I have problem in comparing these two Calendar objects .I need to know which among the two given Calendar Objects is Latest One or the Greater one.I'am using the code below.Please let me know how to compare these objects using the code below.Please consider the case for two Strings boolean comparedate = compareDate("2000/01/01 01:01:00","2001/02/21 12:00:00"); public static boolean compareDate(String date1,String date2) { boolean retVal1 = false; boolean retVal2 = false; int yyyy1 = 0, yyyy2 = 0; int mm1 = 0, mm2 = 0, dd1 = 0, dd2 = 0, ss1 = 0, ss2 = 0; int hh1 = 0, hh2 = 0, min1 = 0, min2 = 0;
Calendar earlierdate = new GregorianCalendar(); Calendar laterdate = new GregorianCalendar(); earlierdate.set(yyyy1, mm1, dd1,hh1,min1,ss1); laterdate.set(yyyy2, mm2, dd2,hh2,min2,ss2); //How should I compare these Objects earlierdate //laterdate.Which is bigger?
}//end compareDate Thanks in advance
Michael Hildner
Ranch Hand
Joined: Oct 13, 2000
Posts: 297
posted
0
Use the SimpleDateFormat to turn Strings into dates, then you can compare the dates. Maybe something like:
Originally posted by Seema Shetty: Hi, I have problem in comparing these two Calendar objects .I need to know which among the two given Calendar Objects is Latest One or the Greater one.I'am using the code below.Please let me know how to compare these objects using the code below.Please consider the case for two Strings boolean comparedate = compareDate("2000/01/01 01:01:00","2001/02/21 12:00:00"); public static boolean compareDate(String date1,String date2) { boolean retVal1 = false; boolean retVal2 = false; int yyyy1 = 0, yyyy2 = 0; int mm1 = 0, mm2 = 0, dd1 = 0, dd2 = 0, ss1 = 0, ss2 = 0; int hh1 = 0, hh2 = 0, min1 = 0, min2 = 0;
Calendar earlierdate = new GregorianCalendar(); Calendar laterdate = new GregorianCalendar(); earlierdate.set(yyyy1, mm1, dd1,hh1,min1,ss1); laterdate.set(yyyy2, mm2, dd2,hh2,min2,ss2); //How should I compare these Objects earlierdate //laterdate.Which is bigger?
}//end compareDate Thanks in advance
Hi, You can try this piece of code for comparing Date d1=earlierdate.getTime(); Date d2=laterdate.getTime(); int result=d1.compareTo(d2); if(result<0) //d1 is earlier<br /> if(result==0)//both are equal<br /> if(result>0)//d2 is earlier [This message has been edited by sai challa (edited April 12, 2001).]
As a corollary; I have a need to parse date strings of the format: 2002/12/14 13:56:01.0 Noticed the precision of the seconds field - with the additional ".0". Will this work ? Because I can only find the meta field "SSS" to represent milliseconds. yyyy/MM/dd HH:mm:ss.SSS Thanks Pho
as a late edition, when dealing with string dates I just compare them lexicographically as Strings. The only assumption is that they have to be in fixed format ( i.e. 1999/01/05 and not 1999/1/5 ). This works for all date/time Strings. The only danger is that you will not catch malformed date exceptions. example:
Any criticisms on my "quick and dirty" method would be appreciated as I us this method regularly. Jamie
as an add on for Pho Tek's milliseconds, it will successfully compare uneven decimal places in the last spot( i.e. xxx.15 compared to xxx.1 ) Jamie [ May 21, 2002: Message edited by: Jamie Robertson ]
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
posted
0
This works for all date/time Strings Not all. It won't work for "May 21, 2002", or "05/21/2002", or "21/05/2002". But yes, provided you make sure you're using a date format which orders all information by decreasing significance (and if as you said the format is fixed-width and you don't need to catch invalid formats), this method can work very well. I'm not sure it justifies eight lines of code though: [ May 21, 2002: Message edited by: Jim Yingst ]
Originally posted by Jim Yingst: This works for all date/time Strings Not all. It won't work for "May 21, 2002", or "05/21/2002", or "21/05/2002". But yes, provided you make sure you're using a date format which orders all information by decreasing significance (and if as you said the format is fixed-width and you don't need to catch invalid formats), this method can work very well. I'm not sure it justifies eight lines of code though: [ May 21, 2002: Message edited by: Jim Yingst ]
Does it really matter whether you check for the -1, 0 or 1 result inside the method or outside? I think it is just more intuitively obvious if you return a boolean telling you whether the condition your checking is true or not. It makes the outside code a little cleaner. just my $.0002 Jamie
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
posted
0
I'm not sure what you're talking about. My version has the exact same usage and behavior as yours does. The check is within the method, which returns a boolean.