Wednesday, November 21, 2018

Java server and client are in different timezones: Convert Timestamp / time to ZonedDateTime for comparison

If you are comparing a Timestamp from a remote server located in a different timezone with a local Timestamp, you need to convert either the remote Timestamp to a local one or vice versa.

You need to know your server's timezone id no matter which way you are going to convert. Let's say your server's timezone id is "America/New_York". (You can get all the available timezone ids by calling TimeZone.getAvailableIDs().)

1. Convert a remote Timestamp to a local ZonedDateTime


          String remoteZoneId = "America/New_York";
          TimeZone remoteTimeZone = TimeZone.getTimeZone(serverZoneId);

          Timestamp remoteTimestamp = <the value to be converted>

          //Firstly, create a ZonedDateTime representing the remote Timestamp

          GregorianCalendar remoteCalendar = new GregorianCalendar(remoteTimeZone);
          remoteCalendar.setTimeInMillis(remoteTimestamp.getTime());
          ZonedDateTime remoteZDT = remoteCalendar.toZonedDateTime();

         // ( OR use the following code to get the remote ZonedDateTime:

         //       Instant remoteInstant = Instant.ofEpochMilli(remoteTimestamp.getTime());
         //       ZonedDateTime remoteZDT = remoteInstant.atZone(remoteTimeZone.toZoneId());
         // )

          //Secondly, convert this remote ZonedDateTime to a local ZonedDateTime

          TimeZone localTimeZone = TimeZone.getDefault();

          ZonedDateTime remoteZonedResult;
          if (remoteTimeZone.getRawOffset() == localTimeZone.getRawOffset()){
                   remoteZonedResult = remoteZDT;
          } else {
                  remoteZonedResult = remoteZDT.withZoneSameInstant(localTimeZone.toZoneId());
          }


2. Convert a local Timestamp to a local ZonedDateTime


          Timestamp localTimeStamp = <your local time stamp>
          TimeZone localTimeZone = TimeZone.getDefault();

          ZonedDateTime localZonedResult = ZonedDateTime.of(localTimeStamp.toLocalDateTime(),           localTimeZone.toZoneId());

3. Comparing


           remoteZonedResult.isBefore(localZonedResult);
           remoteZonedResult.isAfter(localZonedResult);
           remoteZoneResult.equals(localZonedResult);

Of course, you can also convert the local Timestamp to a remote ZonedDateTime, and then comparing it with the remote ZonedDateTime representing a remote Timestamp.

-----------------------------------------------------------------------------------------------------------------
Watch the blessing and loving online channel: SupremeMasterTV live


             

If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book for free here.

No comments:

Post a Comment