there are 2 ways we get browser's timezone from request object.
when you are making request from the browser add an parameter to request object using javascript. The below command gives browser's timezone:
Intl.DateTimeFormat().resolvedOptions().timeZone
using this command you will get an string representing timezone example "Pacific/Fakaofo,Pacific/Honolulu" you can get this time zone out of request object on server side using
String timezoneStr = request.getParameter("your_parameter_name");
passing this string to Timezone.getTimeZone(timezoneStr); will return timezone object for browser's time
Another way of doing so is get the zoneOffset from the request session. Session contains zoneOffset value in integer form you need to get your GMT time out of that. below is the sample:
public static String getGMTSignedZone(HttpServletRequest request)
{
String zoneOffset;
HttpSession session = request.getSession();
zoneOffset = (String)session.getAttribute("timezone");
if(zoneOffset != null && !zoneOffset.equals(""))
{
Integer zMinutes = Integer.valueOf(zoneOffset);
String sign = (zMinutes < 0) ? "+" : "-";
String hourString;
String minString;
if(zMinutes < 0)
{
zMinutes = zMinutes*(-1);
}
// hours 0 to 23
int hours = zMinutes/60;
if(hours > 23)
{
hours = hours/24;
}
if(hours < 10)
{
hourString = "0" + hours;
}
else
{
hourString = "" + hours;
}
//minute conversion
int minutes = zMinutes - (hours*60);
if(minutes < 10)
{
minString = "0" + minutes;
}
else
{
minString = "" + minutes;
}
return ("GMT" + sign + hourString + minString);
}
return zoneOffset;
}
return of above can be easily converted into Timezone using below code:
StringBuffer buffer = new StringBuffer("");
int absOffset = Math.abs(offset);
int hrs = absOffset/60;
int mins = absOffset%60;
buffer.append("GMT").append(offset > 0 ? "-" : "+").append(hrs < 10 ? "0" : "").append(hrs).append(":").append(mins < 10 ? "0" : "").append(mins);
String tzID = buffer.toString();
TimeZone tz = TimeZone.getTimeZone(tzID);
use any of these method's to get timezone and convert your calender object to defined timezone.
out of both the methods seconds dosen't requires any client side code but a lot of validation on server side, and first approach requires small changes on client side and small changes on server side. It is up to you what you prefer.