Solved

compare to Dates only

Posted on 2004-04-08
5
762 Views
Last Modified: 2008-03-03
Hi,

 I have 2 java.util.Date objects
I want to compare those 2 dates
I am using the following statement

int result = stDt.compareTo(endDt);

i am wondering sometimes even though both the dates are same, it's returning -1
I don't understanding why it's returning zero sometimes and -1 sometimes
even though both the dates are same.

Basically i need a function which should comapare 2 dates. it should give me true if both the dates have same yyyy-MM-dd, even though they have different HH:mm:ss.
It should compare only date part, not time part.

i guess you got my requirement.

Is there any such method in Java?

I appreciate your help!

Thanks
0
Comment
Question by:mvkraju
5 Comments
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 10787478
> even though both the dates are same.
are you sure about that?
the "Date" does not only represent a date, but also a time (with hours, minutes, seconds and milliseconds).
0
 
LVL 92

Expert Comment

by:objects
ID: 10787524
If you don't care about the time part then you could use java.sql.Date

int result = new java.sql.Date(stDt.getTime()).compareTo(new java.sql.Date(endDt.getTime()));
0
 
LVL 3

Accepted Solution

by:
JavaInTheMorning earned 20 total points
ID: 10787614
import java.util.Calendar;
...
...
private boolean compareDates(Date date1, Date date2)
{
      Calendar calendar1 = Calendar.getInstance();
      Calendar calendar2 = Calendar.getInstance();
      calendar1.setTime(date1);
      calendar2.setTime(date2);
      return (calendar1.get(Calendar.DATE) == calendar2.get(Calendar.DATE) &&
                 calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH) &&
                 calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR));
}

Calendar API:
http://java.sun.com/j2se/1.3/docs/api/

To add time comparison, use other constants of Calendar.

Enjoy,
Itsik
0
 
LVL 3

Expert Comment

by:JavaInTheMorning
ID: 10787689
0
 
LVL 1

Expert Comment

by:rajuvegesna
ID: 10790060
you cant compare two dates directly, further here is an example which i have done in my old projects..

// systemdate
                 String systDate = Pyear+"/"+Pmonth+"/"+Pdate ;

//user selected date
             String wholepath = year+"/"+month+"/"+day ;

//one fixed date
             String fixDate = "2001/11/24" ;
            
            DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
            
            Date sys = null;
            try {
                  sys = df.parse(systDate);
            } catch (ParseException e) {
                  e.printStackTrace();
            }
            
            Date pre = null;
            try {
                  pre = df.parse(wholepath);
            } catch (ParseException e1) {
                  e1.printStackTrace();
            }
            
            Date pas = null;
            try {
                  pas = df.parse(fixDate);
            } catch (ParseException e1) {
                  e1.printStackTrace();
            }

compare it as,

                        if(pre.after(sys)) {
                        //
                  }
                  else if(pre.before(pas)) {
                        //
                  }
                  else {
                        //
                  }

hope i could help!!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MySQL  on Tomcat 8 70
Glassfish admin console not working 1 34
java example issue 5 26
How to configure empty element in XML Document parser? 15 36
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question