简体   繁体   中英

Reading Client Side Time Zone in Server Side java class

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

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

Client Side :

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

It's important to understand that a Date value doesn't have a time zone. It just represents an instant in time. When you call Date.toString() , that uses the default time zone of the system you're calling it on. That's why you're getting the behaviour you are.

So it looks like the instant itself is being read correctly - but if you're interested in the local time rather than an instant in time, there are two options:

  • Pass the time zone identifier (eg "Europe/Paris") as well as the instant in time
  • Pass just a local date or date/time and don't try to treat it as an instant in time in the first place

It's hard to know exactly which of those is the most appropriate in your situation. We'd need to know more about what the application is doing, and how you're transferring information.

I would recommend using Joda Time instead of the built-in date/time libraries though - it's a much better API which will help you distinguish between local times and instants.

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. in client side ,

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.

After setting time on server, you can call.
Calendar.setTimezone()
Problem is, that you have to know time zone of client.



: My server is running in IST format (By default JVM has taken OS Time Zone). :我的服务器以IST格式运行(默认情况下,JVM已采用OS时区)。

    String inputDateString  = "2013-10-26 09:36:00";
    String timezoneID       = "CST";

    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
    dateFormat.parse(inputDateString+" "+timezoneID);

    DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    formatter.setTimeZone(TimeZone.getTimeZone(dateFormat.getTimeZone().getID()));

    Date outputDate = formatter.parse(inputDateString);
    System.out.println(outputDate);

Sat Oct 26 20:06:00 IST 2013 IST 2013年10月26日20:06:00

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM