在服务器端java类中读取客户端时区 [英] Reading Client Side Time Zone in Server Side java class

查看:1654
本文介绍了在服务器端java类中读取客户端时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个客户端程序,它向服务器端程序发送时间。

我正在使用Callendar对象将时间值从客户端传递到服务器。

我的客户端程序位于斯里兰卡,服务器位于英国。

当我在客户端发送时间(例如:2011-11-21T12:43:41.352 + 05:30)时,通常是在服务器上转换到英国时间的时间(例如:2011-11-21T07) :13:411.352 + 00:00)。

我想要做的是我想在客户端程序中使用时区在客户端程序中发送时间。(简单来说,我想在服务器中读取时间而不用TimeZone转换适用于客户端程序发送的时间。所以在服务器中我想得到时间2011-11-21T12:43:41.352 + 05:30不是2011-11-21T07:13:411.352 +00:00)。

I have a client side program that sends a time to the server side program.
I am using Callendar object to pass the time values from client to server.
My client program is located at the Sri Lanka and the server is located at the UK.
When I am sending the time(Ex : 2011-11-21T12:43:41.352+05:30) in client side, as usually the time converting to UK Time at the server (Ex : 2011-11-21T07:13:411.352+00:00).
What I want to do is I want to get the time that I am sending in the client program with the Time Zone in the client program.(In simple terms, I want to read the time in server without TimeZone conversions applying to the time that is sent by the client side program. So in the server I want to get the time as 2011-11-21T12:43:41.352+05:30 not as 2011-11-21T07:13:411.352+00:00).

我可以知道这可以用java来做,如果是的话怎么做?

Can I know is this possible with java to do and if yes how?

客户端:

Calendar cal1 = Calendar.getInstance();
Date indate = new Date(); // this shows as 2011-11-21T12:43:41.352+05:30
searchDateRange_type0.setStart(cal1);
cal1.setTime(inDate); 

Serever Side:

SearchDateRange_type0 serachDaterange= criterion_type.getSearchDateRange();
checkInDate     = serachDaterange.getStart().getTime(); //I read this as 2011-11-21T07:13:411.352+00:00


推荐答案

这里的问题是,当java客户端日历转换日期时,它也设置了它的时区。如果您要清除它并且只需要从服务器日历对象获取日期,请在客户端设置时区时将客户端日历时间分钟和秒设置为0.

The issue here is that when java client Calendar converting Date its setting up its time zone too. if you will to clear this off and only need to get the date from the server Calendar object , set the client Calendar time minute and seconds to 0 when you set up the time zone in client.

ex。在客户端,

Date indate     = new Date("21-Nov-2011");

Calendar cal1 = Calendar.getInstance();     
cal1.setTime(indate);   

// set the time zone same as server time zone.  
cal1.setTimeZone(TimeZone.getTimeZone("GMT"));

// then clear out the rest in oder to get only the date.
cal1.set(Calendar.HOUR, 0);
cal1.set(Calendar.MINUTE, 0);
cal1.set(Calendar.SECOND, 0);    

因此服务器只能从客户端获得与时区或小时相同的日期。

so the server only get the same date passed from the client with out time zone or the hours.

这篇关于在服务器端java类中读取客户端时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆