how to get client's time zone in java from HttpServletRequest? [duplicate]
Asked Answered
C

2

16

When server and client are in different time zones, can i get client's time zone in java using HttpServletRequest?

I am trying to create an instance of 'Calender' using client's 'Locale' like this,

Calendar calendar = Calendar.getInstance(request.getLocale());
TimeZone clientTimeZone = calendar.getTimeZone();

But this is giving me Server's time zone only.

Is this method wrong? Is there any other way to get Client's time zone in Server?

Culbertson answered 18/3, 2014 at 12:28 Comment(0)
K
11

Unfortunately the time zone information's are not passed in HTTP request.

But there are a work around for this case.

Check this answer and this one. it may help you.

Kiker answered 18/3, 2014 at 12:36 Comment(0)
S
0

there are 2 ways we get browser's timezone from request object.

  1. 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

  1. 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.

Stilliform answered 25/1, 2018 at 15:55 Comment(1)
I prefer the second solution because if avoids moving to the dark side (JavaScript :) ) but unfortunately, in my case, the session doesn't seem to contain the 'timezone' attribute: String zoneOffset = (String)session.getAttribute("timezone"); gives a null zoneOffset, any idea why?Rheumatism

© 2022 - 2024 — McMap. All rights reserved.