We use the following class for date columns:
import org.apache.wicket.WicketRuntimeException;
import
org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.model.IModel;
import org.joda.time.DateTime;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateTimeColumn<T> extends PropertyColumn<T>
{
//**********************************************************************************************************************
// Fields
//**********************************************************************************************************************
private static final long serialVersionUID = -1285893946332340828L;
private final String format;
//**********************************************************************************************************************
// Constructors
//**********************************************************************************************************************
public DateTimeColumn(IModel<String> displayModel, String
propertyExpression, String format)
{
super(displayModel, propertyExpression);
this.format = format;
}
public DateTimeColumn(IModel iModel, String sortProperty, String
propertyExpression, String format)
{
super(iModel, sortProperty, propertyExpression);
this.format = format;
}
//**********************************************************************************************************************
// Other Methods
//**********************************************************************************************************************
@Override
protected IModel createLabelModel(IModel iModel)
{
return new DateTimeModel(super.createLabelModel(iModel));
}
//**********************************************************************************************************************
// Inner Classes
//**********************************************************************************************************************
private class DateTimeModel implements IModel<String>
{
private final IModel inner;
private static final long serialVersionUID = 190887916985140272L;
private DateTimeModel(IModel inner)
{
this.inner = inner;
}
public void detach()
{
inner.detach();
}
public String getObject()
{
DateTime dateTime = (DateTime) inner.getObject();
if(dateTime == null)
{
return "";
}
final Date date = dateTime.toDate();
SimpleDateFormat dateFormatter = new SimpleDateFormat(format);
return dateFormatter.format(date);
}
public void setObject(String s)
{
SimpleDateFormat dateFormatter = new SimpleDateFormat(format);
try
{
Date date = dateFormatter.parse(s);
inner.setObject(new DateTime(date.getTime()));
}
catch (ParseException e)
{
throw new WicketRuntimeException("Unable to parse date.", e );
}
}
}
}
On Fri, Oct 30, 2009 at 10:13 AM, zabian <[email protected]> wrote:
> It worked out.
> My solution is:
> private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
> private static final SimpleDateFormat sdf = new
> SimpleDateFormat(DATE_FORMAT);
>
> ...
> @Override
> protected IModel createLabelModel(IModel rowModel) {
> return new Model<String>(sdf.format(new
> PropertyModel<Date>(rowModel, getPropertyExpression()).getObject()));
> }
>
> Thank you for help.
>
> Regards,
> Wojtek
>
> Ernesto Reinaldo Barreiro pisze:
>>
>> Are you using PropertyColumn? If yes... Why not override
>>
>> protected IModel<?> createLabelModel(IModel<T> rowModel)
>> {
>> return new PropertyModel(rowModel, propertyExpression);
>> }
>>
>>
>> and return a model that formats the date as you need?
>>
>> Best,
>>
>> Ernesto
>>
>> 2009/10/30 zabian <[email protected]>
>>
>>
>>>
>>> Hi there,
>>> i would like to find out if the date format for java.util.Date property
>>> is
>>> customizable in DataTable.
>>> Could anyone point me how to set it?
>>>
>>> Regards,
>>> Wojtek
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: [email protected]
>>> For additional commands, e-mail: [email protected]
>>>
>>>
>>>
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]