如何从HttpServletRequest在java中获取客户端的时区?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22479407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get client's time zone in java from HttpServletRequest?
提问by Noor Khan
When server and client are in different time zones, can i get client's time zone in java using HttpServletRequest?
当服务器和客户端在不同的时区时,我可以使用 HttpServletRequest 在 java 中获取客户端的时区吗?
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?
这个方法有错吗?有没有其他方法可以在服务器中获取客户端的时区?
回答by Salah
回答by Kushagra Misra
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
当您从浏览器发出请求时,使用 javascript 向请求对象添加一个参数。下面的命令给出了浏览器的时区:
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
使用此命令,您将获得一个表示时区示例“Pacific/Fakaofo,Pacific/Honolulu”的字符串,您可以使用以下命令从服务器端的请求对象中获取此时区
String timezoneStr = request.getParameter("your_parameter_name");
passing this string to Timezone.getTimeZone(timezoneStr); will return timezone object for browser's time
将此字符串传递给 Timezone.getTimeZone(timezoneStr); 将返回浏览器时间的时区对象
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; }
另一种方法是从请求会话中获取 zoneOffset。会话包含整数形式的 zoneOffset 值,您需要从中获得 GMT 时间。以下是示例:
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.
在这两种方法中,秒不需要任何客户端代码,但需要在服务器端进行大量验证,第一种方法需要在客户端进行小幅更改,在服务器端进行小幅更改。这取决于你喜欢什么。