0

Default date formatter does not display correctly

asked 2020-10-29 23:14:04 +0800

rmalik gravatar image rmalik
31 1

Hi all,

In our application we have a date that is: 29-jun-2021 0:00:00
When we use the default zk date formatter: @converter('formatedDate', format='dd-MM-yyyy') it displays the following: 28-06-2021

Does anyone know why the date changes? We are using zk version 8.6.0.1 at the moment.

Kind regards,

Remie

delete flag offensive retag edit

6 Answers

Sort by ยป oldest newest most voted
1

answered 2020-10-30 11:06:50 +0800

cor3000 gravatar image cor3000
5409 2 7
ZK Team

updated 2020-10-30 11:14:15 +0800

most likely a time zone (and or DST) issue.

e.g.: if your date at server side is using UTC and is then displayed using "New York" 2021-06-29 0:00 GMT+0 will become 2021-06-28 8:00pm GMT-4

In order to debug simply append the HH:mm:ss Z to the date time format just to see the time and timezone information helping you to debug this in more detail.

<datebox width="300px" format="YYYY-MM-dd HH:mm:ss Z" />
link publish delete flag offensive edit
1

answered 2020-10-30 17:10:34 +0800

MDuchemin gravatar image MDuchemin
1776 1 5
ZK Team

You can make your own converter and pass it from the VM instead of using the default.

http://books.zkoss.org/zk-mvvm-book/8.0/syntax/converter.html

Just implement a class with the converter interface, instantiate one in the VM (with a getter) and pass it to the compoenent:

<datebox value="@load(vm.myDate) @converter(vm.customDateConverter)"/>

If you are using the default converter, you can check ZK's date handling here regarding timezones: https://www.zkoss.org/wiki/ZKDeveloper'sReference/Internationalization/Time_Zone

link publish delete flag offensive edit
0

answered 2020-10-30 16:28:36 +0800

rmalik gravatar image rmalik
31 1

Hi Cor3000,

Yes, that's it! When i use the above setting i get:

2021-06-28 23:00:00 +0100

Is there any way i can add GMT+1 to the converter?

link publish delete flag offensive edit
0

answered 2020-10-30 16:59:59 +0800

rmalik gravatar image rmalik
31 1

I can see i can use the datebox timezone functionality:

<datebox value="@load(line.field('BestBeforeDate').value)" format="YYYY-MM-dd" timezone="Europe/Amsterdam"/>

This works for me. Much appreciated.

link publish delete flag offensive edit
0

answered 2020-10-30 17:36:13 +0800

rmalik gravatar image rmalik
31 1

I see. If i create my own converter it does exactly do what i expected: 2021-06-28. The Date is a CET type and will convert fine.

However looking at https://www.zkoss.org/wiki/ZKDeveloper'sReference/Internationalization/Time_Zone

I change my example after login to:

TimeZone preferredTimeZone = TimeZone.getTimeZone("Europe/Amsterdam");

Executions.getCurrent().getDesktop().setAttribute(Attributes.PREFERREDTIMEZONE, preferredTimeZone);

My datebox: <datebox value="@load(line.field('BestBeforeDate').value)" format="YYYY-MM-dd HH:mm:ss Z"/> still shows: 1-06-28 23:00:00 1.

I expected it to be: 1-06-28 00:00:00

link publish delete flag offensive edit

Comments

There could still be a difference between your client and the server's declared timezone.

MDuchemin ( 2020-10-30 18:37:28 +0800 )edit

possibly daylight saving time, possibly just different timezone.

MDuchemin ( 2020-10-30 18:37:56 +0800 )edit

A good option to find out why this happen would be to put a breakpoint in your VM and check the date object after conversion. This should let you know if the date was converted to the expected timezone, or if the client set it differently initially

MDuchemin ( 2020-10-30 18:39:10 +0800 )edit

Also, depending on how you set the timezone, the setting may not apply before the next page load. A reliable way to set is at server start, with the library property: <library-property> <name>org.zkoss.web.preferred.timeZone</name> <value>time zone here</value> </library-property>

MDuchemin ( 2020-10-30 18:41:11 +0800 )edit

(to expand on that: if you set the timezone for the desktop, the page may have already rendered the timebox as the default timezone, before your change applied)

MDuchemin ( 2020-10-30 18:43:10 +0800 )edit
0

answered 2020-10-30 23:20:16 +0800

rmalik gravatar image rmalik
31 1

Thanks for the info MDuchemin. That is really helpfull. I have now discovered that if i use Sessions.getCurrent(); and set the timezone to Amsterdam, the value gets overriden upon entering a regular VM.

In my Debug console i can see that the value of the session attribute is:

org.zkoss.web.preferred.timeZone -> {[email protected]} "sun.util.calendar.ZoneInfo[id="GMT+01:00",offset=3600000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]"

It seems that the application itself is using the sun package to interfere. I have to investigate is further.

link publish delete flag offensive edit

Comments

I have found it! In our baseOverview it got the TimeZone from the browser. After removing it and replacing it with my code it worked! Thanks very much!

rmalik ( 2020-10-30 23:27:02 +0800 )edit

great to hear that ... timezone issues are sometimes challenging to debug, especially remotely

cor3000 ( 2020-11-02 18:40:15 +0800 )edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2020-10-29 23:14:04 +0800

Seen: 13 times

Last updated: Oct 30

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More