chinnusaccount
chinnusaccount

Reputation: 229

Format datetime string in jsp?

How can I format string1 to string2 in jsp? Please help me resolve this issue

String string1 = "2013-10-22 10:00:00',4";
String string2 = "10.00,4";

Upvotes: 3

Views: 9497

Answers (2)

Will Keeling
Will Keeling

Reputation: 23054

Add the taglib directive to your jsp:

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

You should be able to format the first string to the second string as follows. First convert string1 to an intermediate date:

<c:set var="string1" value="2013-10-22 10:00:00',4" />
<fmt:parseDate value="${string1}" var="theDate" 
     pattern="yyyy-MM-dd HH:mm:ss'',S" />

Then format it to the required format:

<fmt:formatDate value="${thedate}" pattern="hh.mm,S" var="string2"/>

Which you can then use:

<c:out value="${string2}" />

Upvotes: 5

Masudul
Masudul

Reputation: 21981

Use JSTL <fmt:formatDate> tag.

Upvotes: 1

Related Questions