9 Replies Latest reply: Aug 17, 2009 3:52 PM by DrClap RSS

    Need sample Java utility to synchronise the timings on client and server

    807580
      Hello All,

      I need help in writing the Java utility, which will synchronise the Timimgs and timezone between client machine and server.

      Problem Description : When we access the application and we are writing date control values. but It is showing the different date in the database. We have identified that TimeZone issue with server and client. when we keep our client time zone on same as server. things works proper.

      Pls can some one provide Java Utility ,which can compare the Time zone of Client and server and consider the Server as base and makes the changes in date values. This Utility will work Login page of the on the Web Application which is on the tomcat appserver.
        • 1. Re: Need sample Java utility to synchronise the timings on client and serve
          791266
          Send the time as java.util.Date (or the millisecond value that it wraps) and it will work on both sides.
          • 2. Re: Need sample Java utility to synchronise the timings on client and serve
            DrClap
            kajbj wrote:
            Send the time as java.util.Date (or the millisecond value that it wraps) and it will work on both sides.
            Except if you interpret the java.util.Date object as a date (and not as a timestamp) then you can indeed see incorrect information -- it happened in my company.

            The server creates a Date object which was at 00:00:00 on August 17, let's say. This is meant to represent just August 17. Then it sends the object to a client in a timezone two hours farther east, where the client interprets the Date object as 22:00:00 on August 16 and displays it simply as August 16.

            We would never have thought of forcing all the client's machines to be in the same time zone as the server. In our environment (and I think in most environments) that would be completely absurd. I believe we solved this one by passing the server's timezone back to the client as well as the Date object, and then formatting the Date object using that timezone.
            • 3. Re: Need sample Java utility to synchronise the timings on client and serve
              791266
              DrClap wrote:
              kajbj wrote:
              Send the time as java.util.Date (or the millisecond value that it wraps) and it will work on both sides.
              Except if you interpret the java.util.Date object as a date (and not as a timestamp) then you can indeed see incorrect information -- it happened in my company.

              The server creates a Date object which was at 00:00:00 on August 17, let's say. This is meant to represent just August 17. Then it sends the object to a client in a timezone two hours farther east, where the client interprets the Date object as 22:00:00 on August 16 and displays it simply as August 16.

              We would never have thought of forcing all the client's machines to be in the same time zone as the server. In our environment (and I think in most environments) that would be completely absurd. I believe we solved this one by passing the server's timezone back to the client as well as the Date object, and then formatting the Date object using that timezone.
              Ok, I probably need a better description of what the OP means by showing different date in database. Sending the java.util.Date around as a date will work. You can then decide if you want to display that time in local time to the client, or as the time on the server. The millisecond value will however be the same, so you can't say that it is different or wrong. It's how you display it that is wrong :)
              • 4. Re: Need sample Java utility to synchronise the timings on client and serve
                807580
                Can some one provide me the sample code for the same ?
                • 5. Re: Need sample Java utility to synchronise the timings on client and serve
                  796447
                  Mits wrote:
                  Can some one provide me the sample code for the same ?
                  Would you like fries with that, or would you like to try today's specialty: Hushpuppies?
                  • 7. Re: Need sample Java utility to synchronise the timings on client and serve
                    791266
                    Mits wrote:
                    Can some one provide me the sample code for the same ?
                    Code for what? How do you send the date now? Don't you know how to use java.util.Date?
                    • 8. Re: Need sample Java utility to synchronise the timings on client and serve
                      DrClap
                      Mits wrote:
                      Can some one provide me the sample code for the same ?
                      No. All we know is that you have a web application and for some reason there's a problem because the client is not in the same timezone as the server. (Even that is just a fairly strong guess.) We have no idea what this problem is except that apparently it involves a database in some way. You're going to have to ask a specific question and not just whine for code.
                      • 9. Re: Need sample Java utility to synchronise the timings on client and serve
                        jschellSomeoneStoleMyAlias
                        DrClap wrote:
                        kajbj wrote:
                        Send the time as java.util.Date (or the millisecond value that it wraps) and it will work on both sides.
                        Except if you interpret the java.util.Date object as a date (and not as a timestamp) then you can indeed see incorrect information -- it happened in my company.

                        The server creates a Date object which was at 00:00:00 on August 17, let's say. This is meant to represent just August 17. Then it sends the object to a client in a timezone two hours farther east, where the client interprets the Date object as 22:00:00 on August 16 and displays it simply as August 16.

                        We would never have thought of forcing all the client's machines to be in the same time zone as the server. In our environment (and I think in most environments) that would be completely absurd. I believe we solved this one by passing the server's timezone back to the client as well as the Date object, and then formatting the Date object using that timezone.
                        I think all of that needs to be driven by specific requirements. Always.

                        I see no problem at all in insisting that the server will be in a specific time zone and the clients are responsible for correctly resolving time in whatever way that they want. But it must be explicitly spelled out.

                        Any time I see anything at all that looks like a time/date/timestamp anymore I insist that it be formally defined in detail. With timestamps that always means with either a timezone as part of the data or using a fixed timezone.