• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Comparing two date inputs

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author & internet detective
Posts: 41184
848
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tariq,
Java's equals method does that:
cal1.equals(cal2);
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 27286
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Tariq Ahsan
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic