4 Replies Latest reply: Aug 1, 2011 11:41 AM by YoungWinston RSS

    Converting a Hong Kong timezone to TokyoTime zone

    879437
      Hi everyone,

      currently i have a converting issue regarding about timezone.

      My application performs a transaction A and stores the server time (Hong Kong), 10:30:00 p.m into the database.
      Based in what i know, the time zone for Hong Kong is GMT +8.

      However when my Tokyo colleague prints a report based in that transaction. However he does not want to see the time based in Hong Kong. He wants the time to be display in Tokyo time zone.

      Which means when my Tokyo colleague prints the report, he wants the time to be display as 11:30:00 p.m, instead of 10:30:00 p.m as the GMT for Tokyo is GMT +9.

      How can i retrieve the time value from the database and parse/convert the time to Tokyo time zone?

      Is there any java method/wrapper that can convert the one timezone to another?

      Thanks for the advice:)

      Edited by: user12303558 on Aug 1, 2011 8:09 AM
        • 1. Re: Converting a Hong Kong timezone to TokyoTime zone
          836548
          Save/get the time in timestamp and when displaying use locale .
          Which means when my Tokyo colleague prints the report, he wants the time to be display as 11:30:00 p.m, instead of 10:30:00 p.m as the GMT for Tokyo is GMT +9.

          How can i retrieve the time value from the database and parse/convert the time to Tokyo time zone?
          As Ram has mentioned, addition to him do below steps:
          Instead of IST set the timeZone to GMT. And then using getCalendar, add 9 hours to GMT (as tokyo timezone is GMT+9)

          java.text.DateFormat df = new java.text.SimpleDateFormat("HH:mm:ss z");
          df.setTimeZone(java.util.TimeZone.getTimeZone("GMT"));
          System.out.println(df.format(new java.util.Date(dbDate)));

          df.getCalendar().add(java.util.Calendar.HOUR_OF_DAY,9);
          System.out.println(df.getCalendar().getTime());

          Edited by: 833545 on Aug 1, 2011 10:09 PM
          • 2. Re: Converting a Hong Kong timezone to TokyoTime zone
            802316
            For this reason, I would store all timestamps in GMT and convert as required to the timezone desired for presentation.
            If you don't want to change you database you can adjust the time by the difference in timezones.
                 Timestamp tsFromDB = 
                 TimeZone hkTZ = 
                 long gmtTime = tsFromDB.getTime() - hkTZ.getOffset(tsFromDB.getTime());
                 TimeZone tokTZ = 
                 long tokTime = gmtTime + tokTK.getOffset(gmtTime);
            • 3. Re: Converting a Hong Kong timezone to TokyoTime zone
              798692
              Date class doesn't jave any methods related to the timezone. So if you want to format the date based on the timezones, DateFormat class can be used. For more details about DateFormat, refer java [url http://download.oracle.com/javase/6/docs/api/java/text/DateFormat.html]docs.

              For example to display the IST timezone.
              DateFormat df = new SimpleDateFormat("HH:mm:ss z"); 
              df.setTimeZone(TimeZone.getTimeZone("IST"));  
              System.out.println(df.format(dateDB));
              Here dateDB is from data base.
              • 4. Re: Converting a Hong Kong timezone to TokyoTime zone
                YoungWinston
                user12303558 wrote:
                How can i retrieve the time value from the database and parse/convert the time to Tokyo time zone?
                Just to (hopefully) make clear what the others are saying: You never CHANGE the date; you format it for display with a specific timezone. The time (or DATETIME) value remains the same.

                Unless you are using some bizarre database, it will almost certainly store its date/time values as UTC (basically the same thing as GMT), so it's highly unlikely that any conversion is required at all.
                In fact, if your date is being pulled into Java on a Japanese machine with something like JDBC, I'm rather surprised that it isn't displaying Japanese time by default.

                Winston