Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
How to get the client Browser TZ in Vaadin Server
Hi
I need to get the browser time zone on the Vaadin application
I tried
((WebApplicationContext)this.getContext()).getBrowser().getRawTimezoneOffset() etc. methods present in WebBrowser class
but all of them have no values of the client Time Zone and return default values 0 or the server time
Does any one know how to solve this ?
Thanks
Anurag
"default values 0 or the server time" So which one does it return. You're looking in the right spot. There should be a few methods about time zones. They should return the difference in hours between server and client. From there you can use the int to convert it into a timezone with a lookup.
Hi Jens,
I tried ((WebApplicationContext)this.getContext()).getBrowser().getCurrentDate() ... this always returns the Date in the server Time Zone , which does not help.
((WebApplicationContext)this.getContext()).getBrowser().getRawTimezoneOffset() .. this always returns 0 irrespective of the TZ of the browser client ,
which is also not correct
Hence i need to know if someone has solved this or this is a bug in Vaadin.
-Anurag
appears to work just fine. The source code looks similar to yours (link at the top right), have not investigated further.
Hi Jean ,
I am using Vaadin 6.7.3 , i tried the same code & it does not work.
Has this anything to do with vaadin version ??
-Anurag
Where exactly in your code are you trying to retrieve the timezone? The timezone won't be availabe until the first client<->server roundtrip has been made i.e. its not available in the Application constructor for instance.
Thanks John
You were right , i was trying to get hold before the first round trip.
Now it is having valid data.
-Anurag
How did you solve your problem? How do you know if the first "round-trip" is already finished?
Chris,
In my case we have a Login View & subsequently the application view , so we put the necessary code after the Login is done.
By this time the application instance has already been created.
Please try with following codes
getUI().getPage().getWebBrowser().getCurrentDate();
getUI().getPage().getWebBrowser().getTimezoneOffset();
I am having issues with this also. In part of my code I am getting the current datetime and using it in the application
SimpleDateFormat sdfDate = new SimpleDateFormat("MMM dd yyyy HH:mm");
String info = sdfDate.format(getUI().getPage().getWebBrowser().getCurrentDate());
return info
The time I am getting back is one hour before the current time. I have check the time and timezone on both the server and client, and the time setup is correct. When I run in eclipse I do not have this problem and also the popup date time fields have the correct time.
What am I doing wrong, it has got to be something simple?
If you want to retrieve native java's ZoneId, you can do it this way:
int offsetInt = getUI().getPage().getWebBrowser().getTimezoneOffset();
int MILLISECONDS_IN_HOUR = 3_600_000;
int MILLISECONDS_IN_MINUTE = 60000;
int SECONDS_IN_MINUTE = 60;
...
String offset = String.format("%02d:%02d",
Math.abs(offsetInt / MILLISECONDS_IN_HOUR),
Math.abs((offsetInt / MILLISECONDS_IN_MINUTE) % SECONDS_IN_MINUTE));
offset = (offsetInt >= 0 ? "+" : "-") + offset;
ZoneId zoneId = ZoneId.of(offset);
I found this solution here: http://outofrange.ru/2017/01/how-to-get-user-zoneid-in-vaadin/
how can we get exact timezone of client browser
i tried the below on but it not give the actual timezone of client it gives server timezone
WebBrowser webBrowser = UI.getCurrent().getPage().getWebBrowser();
webBrowser.getCurrentDate()