We can get the client timezone using javascript.
First we can get the time offset from client browser, and set it in a cookie.
Than we will access that cookie in our java code, and get the timezone based on that offset.
We will use this timezone in trininad-config.xml
trininad-config.xml settings for getting timezone
<time-zone>#{sessionBean.timeZone}</time-zone>
Than we need to add the getter method for timezone in our SessionBean class.
public TimeZone getTimeZone() { // code here // we will see the internal code in some time }
Now, we will ass javascript in our jspx page, which will set time offset in cookie.
<af:resource type="javascript"> function setTimeZoneInCookie() { var _myDate = new Date(); var _offset = _myDate.getTimezoneOffset(); document.cookie = "TIMEZONE_COOKIE=" + _offset; //Cookie name with value } setTimeZoneInCookie(); </af:resource>
Now, we will see the code for getTimeZone method in sessionBean
public TimeZone getTimeZone() { Calendar now = Calendar.getInstance(); String offsetStr = getOffsetFromCookie(); if (offsetStr == null) { return now.getTimeZone(); } Integer offset = Integer.parseInt(offsetStr); int minutes = Math.abs(offset) % 60; String timeZoneStr = "GMT+" + (-1)*offset/60 + ":" + ((minutes < 10) ? "0" + minutes : minutes); // e.g. GMT + 5:30 TimeZone tz = TimeZone.getTimeZone(timeZoneStr); return tz; } private String getOffsetFromCookie() { FacesContext vFacesContext = FacesContext.getCurrentInstance(); ExternalContext vExternalContext = vFacesContext.getExternalContext(); Map vRequestCookieMap = vExternalContext.getRequestCookieMap(); javax.servlet.http.Cookie vMyCookie = (javax.servlet.http.Cookie)vRequestCookieMap.get("TIMEZONE_COOKIE"); if (vMyCookie != null) { return vMyCookie.getValue(); } return null; }