aspose file tools*
The moose likes Beginning Java and the fly likes How to Compare two Calendar Objects. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to Compare two Calendar Objects." Watch "How to Compare two Calendar Objects." New topic
Author

How to Compare two Calendar Objects.

Seema Shetty
Greenhorn

Joined: Jan 23, 2001
Posts: 9
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;

retVal1 = isDate(date1);

if (retVal1 == true)
{
dd1 = myCalendar.get(myCalendar.DAY_OF_MONTH);
mm1 = myCalendar.get(myCalendar.MONTH) + 1;
yyyy1 = myCalendar.get(myCalendar.YEAR);
hh1 = myCalendar.get(myCalendar.HOUR);
min1 = myCalendar.get(myCalendar.MINUTE);
ss1 = myCalendar.get(myCalendar.SECOND);
}
retVal2 = isDate(date2);
if (retVal2 == true)
{
dd2 = myCalendar.get(myCalendar.DAY_OF_MONTH);
mm2 = myCalendar.get(myCalendar.MONTH) + 1;
yyyy2 = myCalendar.get(myCalendar.YEAR);
hh2 = myCalendar.get(myCalendar.HOUR);
min2 = myCalendar.get(myCalendar.MINUTE);
ss2 = myCalendar.get(myCalendar.SECOND);
}

// long onehr = 60 * 60 * 1000L;

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
Use the SimpleDateFormat to turn Strings into dates, then you can compare the dates. Maybe something like:

[ May 22, 2002: Message edited by: Jim Yingst ]
Art Metzer
Ranch Hand

Joined: Oct 31, 2000
Posts: 241
Hi, Seema.
To make your life a little easier, try using SimpleDateFormat and java.util.Calendar's built-in before() method:

Hope this helps,
Art
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Why not just use the before() or after() methods of the Calendar class?
------------------
Moderator of the JDBC Forum


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
sai challa
Ranch Hand

Joined: Feb 06, 2001
Posts: 54
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;

retVal1 = isDate(date1);

if (retVal1 == true)
{
dd1 = myCalendar.get(myCalendar.DAY_OF_MONTH);
mm1 = myCalendar.get(myCalendar.MONTH) + 1;
yyyy1 = myCalendar.get(myCalendar.YEAR);
hh1 = myCalendar.get(myCalendar.HOUR);
min1 = myCalendar.get(myCalendar.MINUTE);
ss1 = myCalendar.get(myCalendar.SECOND);
}
retVal2 = isDate(date2);
if (retVal2 == true)
{
dd2 = myCalendar.get(myCalendar.DAY_OF_MONTH);
mm2 = myCalendar.get(myCalendar.MONTH) + 1;
yyyy2 = myCalendar.get(myCalendar.YEAR);
hh2 = myCalendar.get(myCalendar.HOUR);
min2 = myCalendar.get(myCalendar.MINUTE);
ss2 = myCalendar.get(myCalendar.SECOND);
}

// long onehr = 60 * 60 * 1000L;

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).]
Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 761

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


Regards,

Pho
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Looks like it would work. Have you tried it?


"I'm not back." - Bill Harding, Twister
Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 761

Jim,
I am trying it out as we speak.
I will let the forum know.
Pho
Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 761

It works.
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

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
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

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: 18671
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 ]
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

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: 18671
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.
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879


I missed the < 0 part of the return
Jamie
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to Compare two Calendar Objects.