0

i'm trying to use DateFormat.

If i write this in a jsp page i have no problem

DateFormat df = new SimpleDateFormat("dd"); 
String print= df.format(new Date());
out.print(print);

If i try to write this in a javabean

public class Date {
String printDate="";    

    public String DataAttualeFormatoItaliano (){

        DateFormat df = new SimpleDateFormat("dd"); 
        printDate=df.format(new Date());
                return  printDate;

    }

}

and in my jsp page i use the javabean this way

<jsp:useBean id="Data" class="Jeans.Date"/>
<%
out.print(Data.DataAttualeFormatoItaliano());
%>

I get this error

org.apache.jasper.JasperException: java.lang.IllegalArgumentException: Cannot format given Object as a Date
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:502)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:430)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

Does anybody know why? Thank you

MDP
  • 4,177
  • 21
  • 63
  • 119
  • 1
    Few links you should really read: [Code Conventions for the Java TM Programming Language](http://www.oracle.com/technetwork/java/javase/documentation/codeconvtoc-136057.html) and [How to avoid Java Code in JSP-Files?](http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files) – sp00m Oct 02 '13 at 07:56

1 Answers1

1

Your class (Date) has same name of java.util.Date, so you're trying to parse an object of your own class into a String ;-). Your must parse a java.util.Date object instead

DateFormat df = new SimpleDateFormat("dd"); 
printDate=df.format(new java.util.Date());
return printDate;
angel_navarro
  • 1,757
  • 10
  • 11