How to format grid column date?

Hello

I am having a problem in formating date in my Grid. My entity is having ZonedDateTime date. So it is looking like "2018-02-01T16:21:35.610-07:00[America/Phoenix]
". But I want it on format “yyyy-MM-dd HH:mm:ss” that means “2018-02-01 16:21:35”.

How to do this?

You could use a Renderer in a column. Have you checked
https://vaadin.com/docs/v8/framework/components/components-grid.html
. Scroll to bottom. There are some renderer samples.

Hi Bhavik

I did it in my grid like this with String.format to show date as: dd/mm/yyyy.

table.addColumn(Deposit::getDate).setId(“dateCol”).setCaption(“Date”).setWidth(85);
table.getColumn(“dateCol”).setRenderer((Renderer) new DateRenderer(“%1$td/%1$tm/%1$tY”));

Here you have more examples:

https://dzone.com/articles/java-string-format-examples

I hope it will be useful.
Aleksander

Hi Johannes Häyry,

Thank you for suggesion. I have tried it but my attribute type is ZoneDateTime so when I am trying renderer it’s giving error.
Is there any other way to do it?

Hi. Sorry I can’t test this code now, but how about using presentation provider and renderer together to strip the zimezone.

Something like:

grid.addColumn(MyPojo::getThatDateTime).setRenderer(zonedDT-> zonedDT.toLocalDateTime(), new LocalDateTimeRenderer());

EDIT: of course the other option is to write yourself a ZonedDateTimeRenderer by extending AbstractRenderer. See how LocalDateTimeRenderer is done.

Thank you for your prompt response on query, I have tried this but it is also not working.

Thank you for your suggesion. I will try it.

Bhavik Vora:

Aleksander Temlin:
Hi Bhavik

I did it in my grid like this with String.format to show date as: dd/mm/yyyy.

table.addColumn(Deposit::getDate).setId(“dateCol”).setCaption(“Date”).setWidth(85);
table.getColumn(“dateCol”).setRenderer((Renderer) new DateRenderer(“%1$td/%1$tm/%1$tY”));

Here you have more examples:
https://dzone.com/articles/java-string-format-examples

I hope it will be useful.
Aleksander

Thank you for your prompt response on query, I have tried this but it is also not working.

Thanks, work for me
grid.getColumn(“dateContract”).setCaption(“Fecha”)
.setRenderer(new DateRenderer((“%1$td-%1$tm-%1$tY”)));

Aleksander Temlin:
Hi Bhavik

I did it in my grid like this with String.format to show date as: dd/mm/yyyy.

table.addColumn(Deposit::getDate).setId(“dateCol”).setCaption(“Date”).setWidth(85);
table.getColumn(“dateCol”).setRenderer((Renderer) new DateRenderer(“%1$td/%1$tm/%1$tY”));

Here you have more examples:
https://dzone.com/articles/java-string-format-examples

I hope it will be useful.
Aleksander

Thank you very much. It Worked well. You really help me
Thanks in advance :slight_smile: