Thursday, July 2, 2009

Date showing wrong in JSF outputText or with dataTable

Some times in , h:outputText with dataTable or without , java.util.Date showing wrong, means one date before the actual date is showing.
We can solve this problem using Converter.
One example :

package com.converter;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
* This class is is used to convert a date to dd-MMM-yyyy format
* @author jijo
public class DateConverter implements Converter {
public String getAsString(FacesContext context, UIComponent c, Object object)
throws ConverterException {
if(object!=null && !object.toString().equals("")){
final Date date = (Date) object;
String dt=null;
SimpleDateFormat formatter=new SimpleDateFormat("dd-MMM-yyyy");
return dt;
return null;
public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2)
throws ConverterException {
return null;


In your jsp page

<h:outputText value="#{item.onDate}" converter="dateConvert" />

1 comment:

abhin said...

this is very helping