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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

JSP displaying formatted date question

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


    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, Registered Users 2 Posts: 3,889 ✭✭✭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, Registered Users 2 Posts: 4,225 ✭✭✭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, Registered Users 2 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, Registered Users 2 Posts: 4,225 ✭✭✭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, Registered Users 2 Posts: 4,225 ✭✭✭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