simple Date comparing, how to

H

HS1

Hello

I want to compare two dates.

One date is from a database in the form of yyyy-mm-dd (such as 2004-11-29).
I know this because I print the date using the function:

System.out.println(databaseDate.toString());

Now I want to use the funtion "before" for comparing myDate and
databaseDate. So I create a new Date

Date myDate = new Date(2004, 11, 23);

And then I use the function myDate.before(databaseDate) to compare them.

if (myDate.before(databaseDate))
...... do something.....


However, this fuction did not work.

I check myDate using myDate.toString(), got:

Fri Dec 23 00:00:00

It can be seen that myDate and databaseDate are in different format.

Could you please help how to compare them

One more thing. If I want to allow users to enter in a JTextField (instead
of a fixed date (2004, 11, 23)) to compare with a databaseDate, what is
simple way to do that???

Thank you
 
J

John B. Matthews

"HS1" <[email protected]> said:
Hello

I want to compare two dates.

One date is from a database in the form of yyyy-mm-dd (such as 2004-11-29).
I know this because I print the date using the function:

System.out.println(databaseDate.toString());

Maybe, maybe not. Your databaseDate probably has a time component
buried in there somewhere; it may be set to midnight. You should
also check what year is actually stored in the database.
Now I want to use the funtion "before" for comparing myDate and
databaseDate. So I create a new Date

Date myDate = new Date(2004, 11, 23);

This constructor for a java.util.Date is deprecated. If you use it
anyway, note that "2004" specifies the year 3904 and "11" specifies
the month December. Consider an instance of GregorianCalendar
instead.
And then I use the function myDate.before(databaseDate) to compare them.

if (myDate.before(databaseDate))
...... do something.....


However, this fuction did not work.

No, the expression "myDate.before(databaseDate)" evaluates to false,
as expected.
I check myDate using myDate.toString(), got:

Fri Dec 23 00:00:00

As midnight of 23-Dec-3904 is not before anytime on 29-Nov-2004,
your code _does_ work.
It can be seen that myDate and databaseDate are in different format.

Could you please help how to compare them

Just initialize myDate with a date (and time) before 29-Nov-2004 at
whatever time is stored with that date in your database.

[...]
 
J

John McGrath

One date is from a database in the form of yyyy-mm-dd (such as
2004-11-29). I know this because I print the date using the function:

System.out.println(databaseDate.toString());

What you are printing is the output from the toString() method, which has
nothing to do with how the data value is stored.

I am guessing that your databaseDate is of type java.sql.Date, although
you did not show anything to verify this. If that is the case, the value
is stored as the number of milliseconds since 1-Jan-1970 12:00 AM UTC.
if (myDate.before(databaseDate))
...... do something.....


However, this fuction did not work.

It should, unless my assumption above is incorrect.
I check myDate using myDate.toString(), got:

Fri Dec 23 00:00:00

It can be seen that myDate and databaseDate are in different format.

No. They just return different formats from the toString() method.
Internally, they are identical.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top