Comparing timezone from client with the server one and get client local time if different

res1 :

In my setup I have the server and the client, I have to know if they are in the same timezone (and so it's the same local time).

The client can send to the server the zone Id (like "Europe/Berlin"), the timezone offset from GMT with daylight saving adjustments (int) or the raw offset without daylight saving adjustments (int).

If the timezone are the same so I can assume (correct me if I am wrong) the client localtime is the same as server localtime, else I have to get the client localtime.

I saw there are several classes in java, ZoneId, ZoneOffset and TimeZone, I am a bit confused which one to use. As for input I was thinking to use the zone id or the offset with daylight saving adjustments.

Thanks for the help

UPDATE: I just want to add that what I mean as server timezone is a reference for me and is not defined as system default. In my case I am considering it as ZoneId.of("Europe/Rome").

akuzminykh :

"If the timezone are the same so I can assume (correct me if I am wrong) the client localtime is the same as server localtime, else I have to get the client localtime.", yes. If the clients timezone equals the servers timezone, both local times are identical.

TimeZone is a class used by the old Java classes for handling dates. Don't use it.

ZoneId is a class from the new java.time package that is much more robust and has a nicer design. It describes a timezone like "Europe/Berlin". This class is used to involve things like daylight saving adjustments in calculation. A ZoneId uses a different ZoneOffset in calculation depending on winter- and summertime. Thus ZoneOffset is the class from the new package to describes specific offsets regardless of other factors, e.g. daylight saving adjustments.

As you are interested in the local time of a timezone, you have to use ZoneId here. You can use ZoneOffset as well but you will loose the information about the specific timezone because multiple timezones can have the same offset.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=355564&siteId=1