Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

finding days of two dates in jsp page

 
deep raj
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
deep raj
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then do it in the servlet, or one of the classes that it instantiates.
 
Rob Spoor
Sheriff
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic