Reading timezone on client side on java server side

I have a client program that sends time to a server program.
I am using a Callendar object to pass time values โ€‹โ€‹from client to server.
My client program is in Sri Lanka, and the server is in the UK.
When I send the time (example: 2011-11-21T12: 43: 41.352 + 05: 30) on the client side, as usual, the time for converting to UK Time on the server (example: 2011-11-21T07: 13: 411,352 +00: 00).
I want to get the time that I send to the client program with the time zone in the client program. (Simply put, I want to read the time on the server without TimeZone conversions applicable to the time it is sent by the client-side program. Therefore, on the server I want to get the time as 2011-11-21T12: 43: 41.352 + 05: 30 not like 2011 -11-21T07: 13: 411.352 + 00: 00). <w>

Can I find out if this is possible with java, and if so, 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 
+2
source share
4 answers

The problem here is that when the java client Calendar converts Date, it also adjusts its timezone. if you do this, and you only need to get the date from the Server Calendar object, set the clientโ€™s calendar time for minutes and seconds to 0 when you configure the time zone on the client.

ex. on the 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 that the server receives only the same date that has passed from the client with the time zone or clock.

+2
source

It is important to understand that Date does not have a time zone. It simply represents a point in time. When you call Date.toString() , which uses the default time zone for the system you are calling it to. That is why you get your behavior.

So, it seems that the moment itself is being read correctly, but if you are interested in local time, not instant time, there are two options:

  • Pass in the time zone identifier (for example, "Europe / Paris") as well as the point in time
  • Pass only a local date or date / time and do not try to treat it as instant time in the first place

Itโ€™s hard to know exactly which one is best for your situation. We need to learn more about what the application does and how you transmit information.

I would recommend using Joda Time instead of the built-in libraries of time and time - this is a much better API that will help you distinguish between local times and moments.

+3
source

After setting the time on the server, you can call.
Calendar.setTimezone ()
The problem is that you need to know the time zone of the client.

+1
source

Converting date and time to server time zone format (example: CST for IST)

Note : My server runs in IST format (by default, the JVM is OS Time Zone).

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

Exit: Sat October 26 20:06:00 IST 2013

0
source

Source: https://habr.com/ru/post/1490218/


All Articles