?
Solved

Java == comparison of Dates and Double (object)

Posted on 2007-10-08
8
Medium Priority
?
1,755 Views
Last Modified: 2013-11-23
I have a question in simple Java comparisons ..can I use == to compare Dates(java.util.Date) and Double (objects)

Can we compare Dates by using ==

someObject.getDate() == someObject2.getDate()  

someObject.getDoubleValue() =someObject2.getDoubleValue()

0
Comment
Question by:micro_learner
8 Comments
 
LVL 11

Accepted Solution

by:
Jeffrey Dake earned 2000 total points
ID: 20038541
No, the correct method would be someObject.getDate().equals(someObject2.getDate())

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Date.html

However, in the case a double you can, because it is a primitive value.
If you have a Double object however use the .equals method
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 20038797
just a hint on using equals method for date object,

if you just want to compare the date and not worry about the time( hour, min, sec... ) they got created, you may have to set 0 for them and do the compare!

and everything said by jman56 is correct!
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 20038921
Another hint: when comparing objects, that is non primitive values,
you are comparing values with .equals() but you are comparing addresses with == !!!

;JOOP!
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 37

Expert Comment

by:zzynx
ID: 20039023
I agree with what has been said above.
The bottom line is this: Always compare two objects (Date's, Double's or whatever) with equals()

A nice example illustrating what sciuriware said is this:


        StringBuffer buf1 = new StringBuffer("Hello");
        StringBuffer buf2 = new StringBuffer("Hello");
        String myStr1 = buf1.toString();
        String myStr2 = buf2.toString();
        if (myStr1 == myStr2)
            System.out.println("1) equal");
        else
            System.out.println("1) not equal");
        if (myStr1.equals(myStr2))
            System.out.println("2) equal");
        else
            System.out.println("2) not equal");

0
 
LVL 5

Expert Comment

by:ebertk
ID: 20072457
You can sue this to compare just the day date month of two dates where date1 and date two are both type Date:

double difference = (new Date(date1.getYear(),date1.getMonth(),date1.getDate(),0,0,0)).getTime() - (new Date(date2.getYear(),date2.getMonth(),date2.getDate(),0,0,0)).getTime();
difference = (int)Math.round(difference/86400000.0);

If the difference is 0, then dates are equal, otherwise provides the number of days between the two dates.

Hope this helps!
0
 
LVL 5

Expert Comment

by:ebertk
ID: 20086995
And - to compare two doubles just use:
d1 == d2

Returns true or false, as explained earlier you can just compare two doubles without using any special code.
0
 
LVL 2

Expert Comment

by:amateur83
ID: 20106107
They are right. To compare double, just use ==. If you wanna compare Double, then use equals. Here's a correct example..

        double firstdouble = 1.23;
        double seconddouble = 1.23;
             Double firstDouble = 3.33;
      Double secondDouble = 3.33;
      
        if(firstdouble == seconddouble)
        {
            System.out.println("comparing double and double using == operator");
        }

      if(firstDouble.equals(secondDouble))
      {
            System.out.println("comparing Double and Double using equals");
      }

you cannot compare double using equals as it will throw you "double cannot be dereferenced" when compile.
0
 
LVL 2

Expert Comment

by:amateur83
ID: 20106265
as for DATE, u can use compareTo to compare them

       Date a = new Date();
      Date b = new Date();
      Date c = new Date(106,3,1);
      System.out.println("a=" + a);
      System.out.println("b=" + b);
      System.out.println("c=" + c);

      if(a.compareTo(b) == 0)
      {
      
            System.out.println("same");

      }

      if(a.compareTo(c) > 0)
      {
            System.out.println(">");
      }
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month14 days, 21 hours left to enroll

840 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