StructuredContentTips

From OpenCms Wiki
Jump to: navigation, search

Formatting a date in a structured content page

The problem is that the contentshow element outputs a LONG for a date field, instead of a formatted string. One approach is to use the JSTL fmt tag library to format the date correctly:


(found on the opencms mailing list : http://mail.opencms.org/pipermail/opencms-dev/2005q3/019274.html )

<%@ page session="false" %>
<%@ taglib prefix="cms" uri="http://www.opencms.org/taglib/cms" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %> 
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
 
 
 
<c:set var="dateString"><cms:contentshow element="Release"/></c:set>
<% 
 java.util.Date date = new java.util.Date(); 
 
date.setTime(Long.parseLong(pageContext.getAttribute("dateString").toString(
))); 
 pageContext.setAttribute("date", date); 
%> 
<fmt:formatDate value="${date}" type="date" pattern="dd.MM.yyyy"  />

The fmt:formatDate command takes the standard date formatting patterns as documented at : http://java.sun.com/docs/books/tutorial/i18n/format/simpleDateFormat.html .

If you do not specify a pattern, it will be localized.

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox