Java == comparison of Dates and Double (object)

Posted on 2007-10-08
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()

Question by:micro_learner
    LVL 10

    Accepted Solution

    No, the correct method would be someObject.getDate().equals(someObject2.getDate())

    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
    LVL 26

    Expert Comment

    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!
    LVL 24

    Expert Comment

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

    LVL 37

    Expert Comment

    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");
                System.out.println("1) not equal");
            if (myStr1.equals(myStr2))
                System.out.println("2) equal");
                System.out.println("2) not equal");

    LVL 5

    Expert Comment

    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!
    LVL 5

    Expert Comment

    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.
    LVL 2

    Expert Comment

    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");

                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.
    LVL 2

    Expert Comment

    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)


          if(a.compareTo(c) > 0)

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    copyEndy  challenge 15 41
    sumHeights  challenge 17 52
    mapShare challenge 13 42
    Eclipse IDE - Cannot copy/paste from console output 8 21
    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…
    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 learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
    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…

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now