my dog learned polymorphism*
The moose likes Beginning Java and the fly likes finding days of two dates in jsp page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "finding days of two dates in jsp page" Watch "finding days of two dates in jsp page" New topic
Author

finding days of two dates in jsp page

deep raj
Ranch Hand

Joined: Dec 11, 2009
Posts: 186
in my jsp page
i want to find out how many days between two dates
i wrote for this as

String s1="2010-12-10";
String s2="2010-12-20";

out.print(s1.compareTo(s2));

it shows -1 means s1 is less than s2 but don't show how many less

so what is the method for finding days?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
Moving this to a different forum, since it has nothing to do with JSP. In fact, quite the opposite, since Java code should not be used in any JSP.


Ping & DNS - my free Android networking tools app
deep raj
Ranch Hand

Joined: Dec 11, 2009
Posts: 186
but this functionality is required in my jsp page

so please give me the solution
not any such that move it to other page like that

when i got my solution i delete this post
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
Calculating a value in a JSP page is quite different from using a value in a JSP. The value can be calculated in some backing bean or servlet, and then put into the page context as attribute.

Start by looking at the SimpleDateFormat class, which has methods to parse a string into a Date object. Then you can work with the Date methods to calculate the difference in days.
deep raj
Ranch Hand

Joined: Dec 11, 2009
Posts: 186
okay i will make bean and calculate date differences in days using two date objects
but the problem is that

the two dates are retrieved from database
so how can these values send to bean class
i think it is not possible

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
Then do it in the servlet, or one of the classes that it instantiates.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

Ulf Dittmer wrote:Then you can work with the Date methods to calculate the difference in days.

I think you mean Calendar methods. Date only has a number of milliseconds since Janyary 1st 1970. Because of nasty things like daylight savings time there are days with 23 or 25 hours so simple calculation does not apply.

Fortunately this question has been asked and answered several times before so a search should provide enough help.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: finding days of two dates in jsp page