Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

JSP displaying formatted date question

Options
  • 20-05-2004 2:24pm
    #1
    Registered Users Posts: 4,222 ✭✭✭


    I'm trying to use the JSP taglib <fmt:formatDate>. The problem i am having is that the date i want to format is got form a bean.

    I can display the date fine using
    <input type=text name="fromdatetime" id="fromdatetime" size="18" value="<jsp:getProperty name="serviceInfo" property="startTime" />" readonly>
    
    but it is displaying it in the wrong format. Its being displayed as "Thu May 20 12:42:25 BST 2004" where i want it displayed in the format "yyyy-MM-dd HH:mm:ss"

    I've tried using
    <input type=text name="fromdatetime" id="fromdatetime" size="18" readonly value="<fmt:formatDate var="date2" value="${<jsp:getProperty name="serviceInfo" property="startTime"/>}" pattern="yyyy-MM-dd HH:mm:ss"  />">
    

    but that gives an error "equal symbol expected"

    any ideas?


Comments

  • Registered Users Posts: 3,886 ✭✭✭cgarvey


    Originally posted by Scruff
    I've tried using
    value="<fmt:formatDate var="date2" value="${<jsp:getProperty name="serviceInfo" property="startTime"/>}" pattern="yyyy-MM-dd HH:mm:ss"
    
    you need to escape the quote in the property= , so something like
    property="startTime \"/> }" pattern=...
    


  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    I don't think you can use a tag (your jsp:getProperty tag in this case) in that context.
    Try this.
    <fmt:formatDate var="date2" value="${serviceInfo.startTime}" pattern="yyyy-MM-dd HH:mm:ss"/>
    


    edit.....
    I'm just guessing that the EL will pick up serviceInfo.startTime here, it should, if it doesn't just do this (don't forget to include the taglib directives!):
    <c:set var="myStartDate" scope="page">
     <jsp:getProperty ...../>
    </c:set>
    <fmt:formatDate var="date2" value="${myStartDate}" pattern="yyyy-MM-dd HH:mm:ss"/>
    


  • Registered Users Posts: 4,222 ✭✭✭Scruff


    ok i tried all you suggested.
    cgarvey, tried escaping
    value="<fmt:formatDate var="date2" value="${<jsp:getProperty name=\"serviceInfo\" property=\"startTime\"/>}
    

    and got the error
    org.apache.jasper.JasperException: <h3>Validation error messages from TagLibraryValidator for fmt</h3><p>165: tag = 'formatDate' / attribute = 'value': An error occurred while parsing custom action attribute "value" 
    with value "${<jsp:getProperty name="serviceInfo" property="startTime"/>}": Encountered "<", expected one of [<INTEGER_LITERAL>, <FLOATING_POINT_LITERAL>, <STRING_LITERAL>, "true", "false", "null", "(", "-", "not", "!", "empty", <IDENTIFIER>]</p>
    


    Enygma, your 1st suggestion threw the error
    org.apache.jasper.JasperException: Unparseable date: "Thu May 20 10:00:40 BST 2004"; - nested throwable: (java.text.ParseException: Unparseable date: "Thu May 20 10:00:40 BST 2004")
    
    is this because it automatically formats the date object returned by the bean as a string?

    and your second suggestion didn't throw an error but the input text box came back empty.

    thanks for the suggestions so far, will try any more ye have:)


  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    Oh yeah, it's because the DateFormat doesn't understand the String representation of the date.
    The formatDate tag will, however, accept a Date object i.e.
    <jsp:useBean id="now" class="java.util.Date"/>
    <fmt:formatDate date="${now}" pattern="hh:MM:ss"/>
    

    HTH


  • Registered Users Posts: 27 Harry Lime


    An alternative to using the formatDate tag would be to modify your bean class, so that the getStartTime() method would return an already formatted String (using java.text.SimpleDateFormat), as opposed to a Date object.


  • Advertisement
  • Registered Users Posts: 4,222 ✭✭✭Scruff


    Originally posted by Harry Lime
    An alternative to using the formatDate tag would be to modify your bean class, so that the getStartTime() method would return an already formatted String (using java.text.SimpleDateFormat), as opposed to a Date object.

    yeah, thought of that beofre i poeted the problem but i'm being a bit stubborn atm and still trying to do it on the JSP. You should be able to handle it in the JSP, i just think that resorting to doing it at the bean level is cheating a bit... :dunno:


  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    if you recode getStartDate in the bean to return a date then my first example will do the trick


  • Registered Users Posts: 4,222 ✭✭✭Scruff


    alright, alright, i give up, i've changed it on the server side, not in the bean but i'm now passing a nicely formatted attribute to the jsp and everything is working fine and dandy.
    still pissed that i couldnt get it done on the jsp itself.
    thanks all for the suggestions


Advertisement