0

This code works as it should (subtracting the days between 2 dates), but it shows as an error in Eclipse and I'm trying to figure out how to clean it up so it doesn't give an error. I didn't write the code, btw....

<c:set var="start" value="${move.moveStart}"/>
<jsp:useBean id="start" type="java.lang.String"/>
<c:set var="end" value="${move.moveEnd}"/>
<jsp:useBean id="end" type="java.lang.String"/>

<%
int days = 0;
try{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date dstart = sdf.parse(start);
Date dend = sdf.parse(end);
long milsecs = dend.getTime() = dstart.getTime();
days = (int)(milsec/(1000*60*60*24));
}catch(ParseException pe){
;
}
%>

I'm getting the errors on the sdf.parse(start) and .(end) that it "cannot be resolved to a variable". What am I missing here?

harryBundles
  • 150
  • 3
  • 15
  • I'm not too up on this, but you seem to have a variable called start and a bean called start. I'm not sure what your beans are for. Same applies to 'end.' My first stab would be to rename the beans to startx and endx to see what happens. – Tony Ennis Mar 20 '12 at 12:21
  • With those beans, I'd expect to see something like start.getStartingValue() be in your code. – Tony Ennis Mar 20 '12 at 12:22

2 Answers2

1

Use following code

   <% pageContext.getAttribute("start"); %>

in scriptlet

When you set the variable using jstl it is in pageContextScope by default so you can grab it from pageContext

jmj
  • 237,923
  • 42
  • 401
  • 438
1

Since you have this object called move, and none of your calculations seem to be dependent on the request, why not add a method getDuration on this object which essentially does

@Transient
public int getDuration() {
    int days = 0;
    try{
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
         Date dstart = sdf.parse(start);
         Date dend = sdf.parse(end);
         long milsecs = dend.getTime() = dstart.getTime();
         days = (int)(milsec/(MILLIS_IN_A DAY)); 
    }catch(ParseException pe){
    ;
    }
    return days;
}

so that you can in your jsp say

<c:out value = "${move.duration}"/>

The getDuration method could be implemented by looking at this answer https://stackoverflow.com/a/3300078/9422

Community
  • 1
  • 1
slipset
  • 2,960
  • 2
  • 21
  • 17