views:

118

answers:

2

Is it possible to change default formatting of Date object in JPS pages? Now I have some kind of formatting and I would like to change formatting without using fmt-tags, because that would mean I would have to type that formatting to all pages and there are many!

+1  A: 

As per my knowledge, it is not possible as JSP will just invoke toString method on the object which you cannot override using settings. You have to do it manually.

Teja Kantamneni
A: 

without using fmt-tags

Best is to create your own custom taglib which does exactly the same as JSTL fmt:formatDate.

But why would you want to reinvent the wheel? Is it the "much" effort of replacing the code in JSP? Well, that's just your job :) It's basically as simple as following:

  1. Drop jstl-1.2.jar in /WEB-INF/lib.
  2. Declare fmt taglib in top of your JSP:

    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
    
  3. Use fmt:formatDate on any bean's java.util.Date property.

    <fmt:formatDate value="${bean.date}" type="date" dateStyle="short" />
    

    Above example will print today's date as 10/01/25 for English locales and for example 25.01.10 for German locales. No worries anymore! :)

You would be almost already finished in the time you spent asking the question and waiting the answer ;)

BalusC