This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to Compare two Calendar Objects.

 
Seema Shetty
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 297
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not just use the before() or after() methods of the Calendar class?
------------------
Moderator of the JDBC Forum
 
sai challa
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like it would work. Have you tried it?
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim,
I am trying it out as we speak.
I will let the forum know.
Pho
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works.
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I missed the < 0 part of the return
Jamie
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic