How to convert a string to date and then compare it with todays date?

Hello.

I want to set a format first for my dates

DateFormate df = new SimpleDateFormate("yyyy-mm-dd");

once this is done then I want to convert any string to date object in the above formate

String str="2001-07-19";

Date d = null;

try{

d = df.parse(s);

}catch(ParseException pe) {

pe.printStackTrace();

}

First of all there is something wrong above,cus what I get for this is

Fri Jan 19 00:07:00 MST 2001

where as it should have been

2001-07-19... to my understanding.

once this part is done I need to get current date in the above set format and compare the

current date and the date I set.

I will appreciate the help.

Thanks

[755 byte] By [me_elf] at [2007-9-26 1:13:37]
# 1
First of all, you need to use yyyy-MM-dd and not yyyy-mm-dd because (as the documentation says) "mm" is minutes and "MM" is months. Second, a Date object has a time component, so trying to compare to "today's date" is going to be messy. However I'm sure you can figure that out.
DrClap at 2007-6-29 0:10:37 > top of Java-index,Core,Core APIs...
# 2

for the output part:

a date is a point in time

the output depends on the format you specify for output

using for example a SimpleDateFormat.

You only specified the format for parsing (which is independent for that of output) so java uses some default format ... see the DateFormat.format() method for details.

for the comparison stuff, I just posted a little code snippet in this forum a few minutes ago.

the hint is: Date.getTime() returns milliseconds after a fixed date

hth Spieler

spieler at 2007-6-29 0:10:37 > top of Java-index,Core,Core APIs...