0

I'm just starting using Spring and I'm quite new in this terms.

I'm having a problem, I need to convert one String to a Date in jsp, and I had thought to use jstl.

I know that the variable has a String which is: "17/03/2016" (I know It because without a format It displays ok)

I have tried all kind of things to be able of watching this String in format date but I have no idea why It's not working

${object.myattribute} <-- It displays correctly "17/03/2016"

But I need to have It on a date mode. So I decided to try the fmt:formatDate option, but I must be doing something wrong 'cause I can't get the desired result. It even does nothing, and if the value is inside a table, It won't display the table.

I have tried many things but no one gives the correct result:

<fmt:formatDate type="both" 
        dateStyle="short" timeStyle="short" 
        value="${object.myattribute}" />

${object.myattribute}



    <fmt:parseDate pattern="yyyy-MM-dd" value="${object.myattribute}" var="parsedStatusDate" />

${formattedStatusDate} ${parsedStatusDate}

This shows no result

 <fmt:formatDate value='${object.myattribute}' pattern='dd/MM/yyyy' var="objMyAttrib" />
                        <c:set var="strDate" value="${object.myattribute}"/>
                        ${strDate}
                        <c:out value="${strDate}"></c:out>

The problem is that no one result can be shown. I suposse that there must be some errors so I can't display this info.

I will continue trying but any idea will be really well received :) I assume that I'm doing something wrong but I can't figure what is what I'm doing wrong or if It can't be done (parse from String to Date in jsp)

Thank you in advance. An example will be also well received.

user4703105
  • 11
  • 1
  • 1
  • 1
  • Why are you parsing the date in the jsp? You should do that in the controller. From where is coming the date? Are you using Spring MVC? – Remy Mellet Mar 24 '15 at 22:38

2 Answers2

1

Try this :

<fmt:parseDate pattern="dd/MM/yyyy" value="${object.myattribute}" var="date" />

and use it like : ${date}

tcharaf
  • 630
  • 1
  • 5
  • 11
0

The problem was that I had to order a dataTable column by the string 25/06/2015 for example, and I need It on this style and no other.

But the dataTable was only ordering the column by the day. As I read that I should pass a date from the controller, I found a solution that worked for me.

What I have done is to pass to the model another string which represents the date but in numeric form. I mean if I had "25/06/2015 " now I will have 20150625. dataTable will know how to order It correctly.

As I needed that "numeric String" to order the table I did something like this

<td> // this one date is used to order the dataTable p class="noVisible">${myObject.numericDate}/p//20150625 // this is the date that we will see in the table. It means that the dataTable will order the column by the first attribute It has in the td. ${myObject.stringDate} //25/06/2015
</td>

I hope this can help anyone who can have this kind of question ^^ even that is not a real solution It works for me

zzzz
  • 1