Comparing Dates in JSP  
Author Message
Kaitee





PostPosted: 2006-5-18 8:57:00 Top

java-programmer, Comparing Dates in JSP I'm fairly new to this so please excuse my ignorance.
What I'm trying to do is compare a date coming from a html form to the
current date and setting a limit on that date to only allow a date that
is two months old or newer.

This is my code:
today=2005-05-17
travel=2005-02-17
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date today = sdf.parse(to_date);
Date travel = sdf.parse(tr_date);
//this is where I expect to get an int that represents how many days
apart these dates are.
//and this is where I get errors when compiling, without this line I
get no errors
int number_of_days = sdf.compareTo(today travel);

I can't even compile this with the last line of code(compareTo),
however I can without that line.
I'm getting ')' expected
Can anyone tell me what I'm doing wrong?

 
Kaitee





PostPosted: 2006-5-18 9:05:00 Top

java-programmer >> Comparing Dates in JSP Sorry I missed a couple ;

This is what my code really is:
This is my code:
String today=2005-05-17;
String travel=2005-02-17;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date today = sdf.parse(to_date);
Date travel = sdf.parse(tr_date);
//this is where I expect to get an int that represents how many days
apart these dates are.
//and this is where I get errors when compiling, without this line I
get no errors
int number_of_days = sdf.compareTo(today travel);

 
Kaitee





PostPosted: 2006-5-18 9:07:00 Top

java-programmer >> Comparing Dates in JSP Darn maybe I should have just copied and pasted

This is what my code really is:
String today="2005-05-17";
String travel="2005-02-17";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date today = sdf.parse(to_date);
Date travel = sdf.parse(tr_date);
//this is where I expect to get an int that represents how many days
apart these dates are.
//and this is where I get errors when compiling, without this line I
get no errors
int number_of_days = sdf.compareTo(today travel);

 
 
Matt Humphrey





PostPosted: 2006-5-18 9:57:00 Top

java-programmer >> Comparing Dates in JSP
"Kaitee" <email***@***.com> wrote in message
news:email***@***.com...
> Darn maybe I should have just copied and pasted
>
> This is what my code really is:
> String today="2005-05-17";
> String travel="2005-02-17";
> SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
> Date today = sdf.parse(to_date);
> Date travel = sdf.parse(tr_date);
> //this is where I expect to get an int that represents how many days
> apart these dates are.

You can't reuse the variable today or travel to refer to a Date instead of a
String--you need new variables. You are also referring to variables that
are not defined here--to_date and tr_date. I think you want

String to_date="2005-05-17";
String tr_date="2005-02-17";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date today = sdf.parse(to_date);
Date travel = sdf.parse(tr_date);


> //and this is where I get errors when compiling, without this line I
> get no errors
> int number_of_days = sdf.compareTo(today travel);

SimpleDateFormat does not have a compareTo method. A Calendar (a kind of
Date) does have one that does what you want. You need to convert your dates
into calendar days, something like

Calendar to_cal = GregorianCalendar.getInstance();
to_cal.setTime (to_date);
Calendar tr_cal = GregorianCalendar.getInstance();
tr_cal.setTime (tr_date);

int millisecondsDifference = to_cal.compareTo (tr_cal);

This is the result of subtracting tr_cal from to_cal, expressed in
milliseconds. You will need to work out which comparision direction you
want and convert it to days yourself.

A shorter technique may be to subtract the dates directory, as in
int millisecondsDifference = (int)(today.getTime() - travel.getTime()), but
I recall this having inaccuracies if the dates are not both within the same
daylight savings phase--I always use Calendar objects to manipulate dates
and Date just to hold the value.

Cheers,
Matt Humphrey email***@***.com http://www.iviz.com/



 
 
Kaitee





PostPosted: 2006-5-19 4:11:00 Top

java-programmer >> Comparing Dates in JSP Thanks for the help. Since this application will only be used in
Alaska and I was having problems with the Calendar compareTo function.
I ended up using the shorter technique you gave me. It's working good
enough for me.

Here's the final code:
String to_date="2006-05-18";
String tr_date="2006-05-10";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date today = sdf.parse(to_date);
Date travel = sdf.parse(tr_date);
int millisecondsDifference = (int)(today.getTime() - travel.getTime());

int days_apart = millisecondsDifference/86400000;

 
 
Kunkhmer





PostPosted: 2006-6-18 12:26:00 Top

java-programmer >> Comparing Dates in JSP Kaitee wrote:
> I'm fairly new to this so please excuse my ignorance.
> What I'm trying to do is compare a date coming from a html form to the
> current date and setting a limit on that date to only allow a date that
> is two months old or newer.
>
> This is my code:
> today=2005-05-17
> travel=2005-02-17
> SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
> Date today = sdf.parse(to_date);
> Date travel = sdf.parse(tr_date);
> //this is where I expect to get an int that represents how many days
> apart these dates are.
> //and this is where I get errors when compiling, without this line I
> get no errors
> int number_of_days = sdf.compareTo(today travel);
>
> I can't even compile this with the last line of code(compareTo),
> however I can without that line.
> I'm getting ')' expected
> Can anyone tell me what I'm doing wrong?
>
>

int number_of_days = sdf.compareTo(today travel);

You've got the wrong syntax there, your code is you are trying to comare
the SimpleDateFormat not the Date.
It should be like this

int number_of_days = today.compareTo(travel);


--
Cheers,
Kunkhmer
http://kunkhmer.googlepages.com
 
 
Matt Humphrey





PostPosted: 2006-6-19 1:14:00 Top

java-programmer >> Comparing Dates in JSP
"Kunkhmer" <email***@***.com> wrote in message
news:4494df27$email***@***.com...
> Kaitee wrote:
>> I'm fairly new to this so please excuse my ignorance.
>> What I'm trying to do is compare a date coming from a html form to the
>> current date and setting a limit on that date to only allow a date that
>> is two months old or newer.
>>
>> This is my code:
>> today=2005-05-17
>> travel=2005-02-17
>> SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
>> Date today = sdf.parse(to_date);
>> Date travel = sdf.parse(tr_date);
>> //this is where I expect to get an int that represents how many days
>> apart these dates are.
>> //and this is where I get errors when compiling, without this line I
>> get no errors
>> int number_of_days = sdf.compareTo(today travel);
>>
>> I can't even compile this with the last line of code(compareTo),
>> however I can without that line.
>> I'm getting ')' expected
>> Can anyone tell me what I'm doing wrong?
>>
>>
>
> int number_of_days = sdf.compareTo(today travel);
>
> You've got the wrong syntax there, your code is you are trying to comare
> the SimpleDateFormat not the Date.
> It should be like this
>
> int number_of_days = today.compareTo(travel);

The compareTo method does not return the difference in time for either Date
or Calendar objects. It simply returns -1 if the parameter is earlier than
the object, +1 if it's greater and 0 if they refer to exactly the same point
in time. In my message a month ago I said that the calendar version
appeared to return the difference in milliseconds but I have no idea how I
misread the Calendar javadocs to come to that conclusion. Here's a test
program to show that compareTo (Date and Calendar) is not doing time
difference.

SimpleDateFormat sdf = new SimpleDateFormat ("MM-dd-yyyy HH:mm:ss");

Date d1 = sdf.parse("05-03-2005 10:10:22");
Date d2 = sdf.parse("05-03-2004 10:10:22");

int dc = d1.compareTo (d2);
System.out.println ("d1 vs. d2 = " + dc);

Calendar c1 = GregorianCalendar.getInstance ();
c1.setTime (d1);

Calendar c2 = GregorianCalendar.getInstance ();
c2.setTime (d2);

int cc = c1.compareTo (c2);
System.out.println ("c1 vs. c2 = " + cc);

Cheers,
Matt Humphrey email***@***.com http://www.iviz.com/


 
 
Kunkhmer





PostPosted: 2006-6-19 21:22:00 Top

java-programmer >> Comparing Dates in JSP Matt Humphrey wrote:
> "Kunkhmer" <email***@***.com> wrote in message
> news:4494df27$email***@***.com...
>
>> Kaitee wrote:
>>
>>> I'm fairly new to this so please excuse my ignorance.
>>> What I'm trying to do is compare a date coming from a html form to the
>>> current date and setting a limit on that date to only allow a date that
>>> is two months old or newer.
>>>
>>> This is my code:
>>> today=2005-05-17
>>> travel=2005-02-17
>>> SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
>>> Date today = sdf.parse(to_date);
>>> Date travel = sdf.parse(tr_date);
>>> //this is where I expect to get an int that represents how many days
>>> apart these dates are.
>>> //and this is where I get errors when compiling, without this line I
>>> get no errors
>>> int number_of_days = sdf.compareTo(today travel);
>>>
>>> I can't even compile this with the last line of code(compareTo),
>>> however I can without that line.
>>> I'm getting ')' expected
>>> Can anyone tell me what I'm doing wrong?
>>>
>>>
>>>
>> int number_of_days = sdf.compareTo(today travel);
>>
>> You've got the wrong syntax there, your code is you are trying to comare
>> the SimpleDateFormat not the Date.
>> It should be like this
>>
>> int number_of_days = today.compareTo(travel);
>>
>
> The compareTo method does not return the difference in time for either Date
> or Calendar objects. It simply returns -1 if the parameter is earlier than
> the object, +1 if it's greater and 0 if they refer to exactly the same point
> in time. In my message a month ago I said that the calendar version
> appeared to return the difference in milliseconds but I have no idea how I
> misread the Calendar javadocs to come to that conclusion. Here's a test
> program to show that compareTo (Date and Calendar) is not doing time
> difference.
>
> SimpleDateFormat sdf = new SimpleDateFormat ("MM-dd-yyyy HH:mm:ss");
>
> Date d1 = sdf.parse("05-03-2005 10:10:22");
> Date d2 = sdf.parse("05-03-2004 10:10:22");
>
> int dc = d1.compareTo (d2);
> System.out.println ("d1 vs. d2 = " + dc);
>
> Calendar c1 = GregorianCalendar.getInstance ();
> c1.setTime (d1);
>
> Calendar c2 = GregorianCalendar.getInstance ();
> c2.setTime (d2);
>
> int cc = c1.compareTo (c2);
> System.out.println ("c1 vs. c2 = " + cc);
>
> Cheers,
> Matt Humphrey email***@***.com http://www.iviz.com/
>
>
>
I think it return the number of day different. (although I didn't write
a code to test it)


compareTo

public int *compareTo*(Date <cid:email***@***.com> anotherDate)

Compares two Dates for ordering.

*Parameters:*
|anotherDate| - the |Date| to be compared.
*Returns:*
the value |0| if the argument Date is equal to this Date; a
value less than |0| if this Date is before the Date argument;
and a value greater than |0| if this Date is after the Date
argument.
*Since:*
1.2


--
Cheers,
kunkhmer.googlepages.com
 
 
Matt Humphrey





PostPosted: 2006-6-20 1:51:00 Top

java-programmer >> Comparing Dates in JSP
"Kunkhmer" <email***@***.com> wrote in message
news:4496ae52$email***@***.com...

<snip lead-up>

> I think it return the number of day different. (although I didn't write a
> code to test it)
>
>
> compareTo
>
> public int *compareTo*(Date <cid:email***@***.com>
> anotherDate)
>
> Compares two Dates for ordering.
>
> *Parameters:*
> |anotherDate| - the |Date| to be compared. *Returns:*
> the value |0| if the argument Date is equal to this Date; a
> value less than |0| if this Date is before the Date argument;
> and a value greater than |0| if this Date is after the Date
> argument.
> *Since:*
> 1.2

I don't see anything in this description that says the result is the number
of days difference. The hyphen in the parameters section is a separator,
not a minus sign. compareTo is a method of the Comparable interface and
requires only the result of < 0 for less than, 0 for equal and 1 for
greater.

Although it seems plausible that Date or Calendar could implement compareTo
to return the difference in days to fulfill the compareTo contract, this
would not work because compareTo is used to order all Dates, which are
really timestamps. Two dates on the same day but at different times must
still produce the correct order. Only dates (timestamps) that are equal
should produce 0.

In any case, I *did* write a test case (see prior message) that
demonstrates that compareTo does not give days difference. By a fluke,
compareTo will produce false days difference when you test it with
consequtive days because these will return +1 or -1 for the adjacent days.

Cheers,
Matt Humphrey email***@***.com http://www.iviz.com/