jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes Comparing two date inputs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Comparing two date inputs" Watch "Comparing two date inputs" New topic
Author

Comparing two date inputs

Tariq Ahsan
Ranch Hand

Joined: Nov 03, 2003
Posts: 116
Hello,

I am trying to get the result as a boolean value if one date input (e.g. 11/28/2006 03:50:44 PM) is less than or equal to the other input date (e.g. 11/28/2006 03:50:44 AM) using java.util.Calendar or java.util.GregorianCalendar.
Would appreciate if I could get some pointers to do this.

Thanks.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30370
    
150

Tariq,
Java's equals method does that:
cal1.equals(cal2);


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

In general, you can compare two objects if the class implements the Comparable interface. Then the class must implement a method called compareTo(). The method compareTo returns negative numbe for less than, zero for equals, and a positive number for greater than.

I believe that compareTo() is for the normal ordering and that if you want to use an alternative ordering, you implement the Compartor interface and implement a method called compare() that is like compareTo(), but takes two explicit parameters rather than an imiplicit parameter and one explicit paramater.

Anybody please correct me if I'm wrong.

-- Kaydell
Tariq Ahsan
Ranch Hand

Joined: Nov 03, 2003
Posts: 116
Do I need to convert the date objects into milliseconds from the epoch then use either Calendar's 'equals' method or something like that? Any code snippet for this would be greatly appreciated?

Thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

If you have Date objects then you can call their after(Date) or before(Date) methods. No need for Calendar objects (unless this is some kind of homework assignment that says you must use Calendar objects).
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
There are many options. Unfortunately the equals() method won't help here - it tells you if the two times are equal, but it doesn't tell you whether one date is before or after the other. You can use compareTo() - if you look at the Calendar API you will see that Calendar already implements Comparable, and has a compareTo() method - so you don't need to convert to anything else. You certainly can convert to Date, and use Date's compareTo(), or you can convert to milliseconds (as a log) and do simple math. Theses options should all give you the same answer - choose whichever makes the most sense to you.


"I'm not back." - Bill Harding, Twister
Tariq Ahsan
Ranch Hand

Joined: Nov 03, 2003
Posts: 116
Here what I have from the suggestions I got which seems to work well except for when I have AM or PM instead.

import java.util.Date;
import java.text.SimpleDateFormat;


public class TestDateComparison {

public static void main (String[] args) throws Exception
{

/*String dateStr1 = args[0];
String dateStr2 = args[1];*/

String dateStr1 = "05/26/2006 04:11:13 GMT-06:00 ";
String dateStr2 = "05/26/2006 02:11:13 GMT-08:00 ";

/*String dateStr1 = "05/26/2006 04:11:13 AM";
String dateStr2 = "05/26/2006 04:11:13 PM";*/

Date date1 = new Date();
Date date2 = new Date();

/*SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a"); */

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z");


date1 = sdf.parse(dateStr1);
date2 = sdf.parse(dateStr2);

System.out.println("\n\ndate1 : <" + date1 + "> date2 : <" + date2 + ">\n\n");

System.out.println("Return Value is <" + DateComparison(date1, date2) + ">\n\n");
}

public static int DateComparison (Date date1, Date date2)
{
int retVal = 0;
date1.compareTo(date2);
return retVal;
}
}

Any idea how I can handle this AM/PM issue?

Thanks
 
 
subject: Comparing two date inputs