This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes compare two dates thru Calendar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "compare two dates thru Calendar " Watch "compare two dates thru Calendar " New topic

compare two dates thru Calendar

kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1371
How can i compare two dates thru Calendar ? one is System date and another is Util date. Date format is MM-dd-yyyy:hh:mm:ss
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074

What do you mean with "one is System date and one is Util date"? There is a Date class in package java.util. Do you mean that you have a java.util.Date object and you want to compare it to the current system date?

You can do that like this:

- Get two Calender objects using the Calendar.getInstance() method.
- Set one of the two to the Date object that you have by using the setTime() method.
- Use the before() or after() methods to compare the Calendars.

Lookup the API documentation of java.util.Calendar for more details.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3702

One common way to compare dates (not neccessarily the best but probably the most often used way) is to convert them into their long values after the epoch using such as:

Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
long time = date.getTime();

Once you have long value in milliseconds you can just compare/compute them as numbers.

As long its not the year 2038, you'll be fine.


My Blog: Down Home Country Coding with Scott Selikoff
Consider Paul's rocket mass heater.
subject: compare two dates thru Calendar
Similar Threads
JSF EL Date Functions
comparing dates
date problem
comparing dates
Date vs GregorianCalender