Date format in DataTable

View: New views
7 Messages — Rating Filter:   Alert me  

Date format in DataTable

by zabian :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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: users-unsubscribe@...
For additional commands, e-mail: users-help@...


Re: Date format in DataTable

by reiern70 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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 <zabian99@...>

> 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: users-unsubscribe@...
> For additional commands, e-mail: users-help@...
>
>

Re: Date format in DataTable

by zabian :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I do use PropertyColumn. I will try it out and let you know.
Thanks for help.

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 <zabian99@...>
>
>  
>> 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: users-unsubscribe@...
>> For additional commands, e-mail: users-help@...
>>
>>
>>    
>
>  


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...


Re: Date format in DataTable

by reiern70 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

or

public void populateItem(Item<ICellPopulator<T>> item, String componentId,
IModel<T> rowModel)
{
item.add(new Label(componentId, createLabelModel(rowModel)));
}

and you create directly a the label with the formated date

Ernesto


On Fri, Oct 30, 2009 at 1:55 PM, zabian <zabian99@...> wrote:

> I do use PropertyColumn. I will try it out and let you know.
> Thanks for help.
>
> 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 <zabian99@...>
>>
>>
>>
>>> 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: users-unsubscribe@...
>>> For additional commands, e-mail: users-help@...
>>>
>>>
>>>
>>>
>>
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@...
> For additional commands, e-mail: users-help@...
>
>

Re: Date format in DataTable

by zabian :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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 <zabian99@...>
>
>  
>> 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: users-unsubscribe@...
>> For additional commands, e-mail: users-help@...
>>
>>
>>    
>
>  


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...


Re: Date format in DataTable

by James Carman-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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 <zabian99@...> 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 <zabian99@...>
>>
>>
>>>
>>> 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: users-unsubscribe@...
>>> For additional commands, e-mail: users-help@...
>>>
>>>
>>>
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@...
> For additional commands, e-mail: users-help@...
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...


Re: Date format in DataTable

by reiern70 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

If you have to do the same trick in several places maybe it would be better
to use James' solution. Or role a classes like

public abstract class FormatedDateModel extends Model<String> {
 private static final long serialVersionUID = 1L;
 private IModel<Date> model;
 public FormatedDateModel(IModel<Date> model) {
this.model = model;
}
 @Override
public String getObject() {
return getFormat().format(model.getObject());
}
 protected abstract DateFormat getFormat();
}
 public class MyFormatedDateModel extends FormatedDateModel {
 private static final long serialVersionUID = 1L;
 public FormatedDateModel(IModel<Date> model) {
super(model)
}
 protected DateFormat getFormat(){
return DATE_FORMAT;
}
}

So, that you have

@Override
protected IModel createLabelModel(IModel rowModel) {
          return new MyFormatedDateModel (new PropertyModel<Date>(rowModel,
getPropertyExpression()));
}

Best,

Ernesto

On Fri, Oct 30, 2009 at 3:13 PM, zabian <zabian99@...> 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 <zabian99@...>
>>
>>
>>
>>> 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: users-unsubscribe@...
>>> For additional commands, e-mail: users-help@...
>>>
>>>
>>>
>>>
>>
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@...
> For additional commands, e-mail: users-help@...
>
>