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.

Monday, November 12, 2018

Crystal Reports: Retrieve a license key from a local machine

1. Open the SAP Crystal Reports console.

2. Click at the Help on the top menu and select License Manager



3. Write down the first part of the Existing key codes that is displayed somewhere you can reference later.



4. Click at the Window icon at the bottom-left corner of your computer monitor, type regedit, and open the regedit.

5. In the Registry Editor window, select the HKY_LOCAL_MACHINE in the left panel.


6. Type Ctrl+F to open the Find window. In the Find what slot, type the part of key code that you have saved in step 3. Click the Find Next button. The search may take some time to accomplish.

7. After the search finishes, read the complete license key from the right panel of the Registry Editor.

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

Reference:
1. Crystal Reports FAQ