checking two dates

How to check for that two dates are equal.
One date is stored in the database (DB2) and the other would be supplied by the user from the browser.  I want to check that the two dates should be
equal and then act accordingly.
would I need to make the user to enter date in a specific format or the dates can be enterd in any fomat and then change the input in the format
that is in DB2, if so how to proceed with this.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

This really depends on the type of the field being used to store the date in your Database, and what database you are using. Some types represents the date by using a long to store the number of seconds since Jan 1 1970.

So, probably the easiest way to solve this is to convert and store both the date comming from the user and the date comming from the database into a Date datatype. Then do your comparison using the equiv operator.

Looks like the contructor for the Date has two forms.

Date(int year, int month, int day)


Date(long date).

So, depending on how the date is stored in your database, you may be able to use the Date(long date).

Normaly for user data you should allow the user to enter the date in a human readable format. 12/01/2000 or 1212000. So check out the following Date method to help you convert the user input.


public String toString()

      Formats a date in JDBC date escape format.
            toString in class Date
            a String in yyyy-mm-dd format

Normally you should state why you rejected the answer. The answer provided is one way to compar two dates. So, provide some verbage so I can better understand your problem.

For comparing date, try using the format YYYYMMDD. As for the input, you may use whichever format to accept the date, as long as the date is converted to the suggested format for comparison.

You may want to try these:

1. Create a class that accept two parameters (of String type)
2. Format the date (whether it is from DB2 or user-entered) into the format YYYYMMDD.
3. Pass in these two date String into the class.
4. Perform the date comparison.
5. You may then act accordingly based on the return value.

The above assumes that the date validation has been performed.

The class could be coded as these:

public class CompareDate {
    public static boolean isEqual(String date1, String date2) {
        if date1.equals(date2) {
            return true;
        } else {
            return false;

Possible usage:
String d1 = "20000118";  // either user-input OR from DB2
String d2 = "20000119";  // either user-input OR from DB2
if CompareDate.isEqual(d1,d2) {
    <actions if dates are equal>
} else {
    <actions if dates are not equal>

Hope this answer your question.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.