[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

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.

Thanks.
0
qader99
Asked:
qader99
  • 4
1 Solution
 
dhymesCommented:
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.

Regards,
Dave
0
 
dhymesCommented:
Looks like the contructor for the Date has two forms.

Date(int year, int month, int day)

and

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.

toString

public String toString()

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

0
 
dhymesCommented:
0
 
dhymesCommented:
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.


Regards,
Dave
0
 
tkteeCommented:
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.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now