Results 1 to 8 of 8
  1. #1
    anjibman is offline Member
    Join Date
    Dec 2010
    Posts
    48
    Rep Power
    0

    Default Test equality of two dates in different formats

    Hi All,

    I need to find out if two dates are equal or not. I have two dates coming from UI and from DB. From UI I am getting as Sun Jun 15 00:00:00 CDT 2014 from DB I am getting as 2014-06-15 00:00:00.0. Data type for both fields are java.util.Date but when I do uiDate.equals(dbDate) it return false. So how can I test these dates to fins out they are equal or not?

    Thanks

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Test equality of two dates in different formats

    Can you make a small, complete program (without a DB) that compiles, executes and shows the problem for testing?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    anjibman is offline Member
    Join Date
    Dec 2010
    Posts
    48
    Rep Power
    0

    Default Re: Test equality of two dates in different formats

    In UI from
    Java Code:
    <form:input id="myDate" path="myDate" class="circularTextBox sixDigitDate" title="mm/dd/yyyy" required="required" pattern="(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d" />
    Back end Dto
    Java Code:
    import java.util.Date;
    public class MyEditDto implements Serializable {
    	@DateTimeFormat(pattern="MM/dd/yyyy")
    	@NotNull
    	private Date myDate;
            ...
    }
    Entity class
    Java Code:
    import java.util.Date;
    @Entity
    public class MyEntity extends Event {	
    	private Date myEntDate;
            .....
    }
    Service class
    Java Code:
    MyEntity existingEnt = myRepository.findOne(myKey);
    if(existingEnt .getMyEntDate().equals(myEditDto .getMyDate()) {
       System.out.println("Dates are equal);
    } else {
       System.out.println("Dates are not equal);
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Test equality of two dates in different formats

    Can you make a small, complete program (without a DB) that compiles, executes and shows the problem for testing?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    anjibman is offline Member
    Join Date
    Dec 2010
    Posts
    48
    Rep Power
    0

    Default Re: Test equality of two dates in different formats

    I will write a executable program but for now can anyone tell me why I have different date format with same Date type?
    And also I don't see a way to convert these Date type in same format like parse(String).

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Test equality of two dates in different formats

    why I have different date format with same Date type?
    If you have two java.util.Date objects, where are the different formats?

    way to convert these Date type in same format
    See the SimpleDateFormat class.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    anjibman is offline Member
    Join Date
    Dec 2010
    Posts
    48
    Rep Power
    0

    Default Re: Test equality of two dates in different formats

    found the way to test .getTime() did the trick.
    Java Code:
    if(existingEnt .getMyEntDate().getTime() == myEditDto .getMyDate().getTime()) {
       System.out.println("Dates are equal);
    } else {
       System.out.println("Dates are not equal);
    }

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Test equality of two dates in different formats

    The usual way would be to use the equals() method.
    Which, incidentally, does what you're doing, but it is still the normal way to check equality for objects.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Integer Dates to String Dates Problem
    By HelpingIsCaring in forum New To Java
    Replies: 32
    Last Post: 08-23-2013, 01:29 AM
  2. Equality of BluetoothDevices
    By kjkrum in forum Android
    Replies: 1
    Last Post: 04-16-2013, 08:47 AM
  3. Programming Question - Equality
    By Huntersgirl12 in forum New To Java
    Replies: 7
    Last Post: 01-19-2012, 05:05 PM
  4. Using Booleans for equality?
    By Maniacmike3128 in forum New To Java
    Replies: 1
    Last Post: 04-08-2011, 09:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •