10 Replies Latest reply: Jun 23, 2011 12:28 AM by EJP RSS

    Re: TimeZone is off by one hour

    870505
      Ok... need some help here:

      Right now LA is on PDT. Here is a simply program:
      import java.util.TimeZone;
      public class TimeZoneTest {
           public static void main(String[] args) {
                TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
                System.out.println(tz.getDisplayName());
           }
      }
      It prints out:

      Pacific Standard Time

      Though it should be printing out:

      Pacific Daylight Time

      What am I doing wrong here?
        • 1. Re: TimeZone is off by one hour
          DarrylBurke
          Moderator action: Branched from TimeZone is off by one hour

          Moderator advice: Please don't post to threads which are long dead, and don't hijack another poster's thread. When you have a question, start your own thread. Feel free to post a link to a related thread.

          Also, read the announcements at the top of the forum listing and the forums FAQ linked from every page. They are there for a purpose. Then edit your post and format the code correctly.

          db
          • 2. Re: TimeZone is off by one hour
            DarrylBurke
            Cross posted (which explains the uppercase code tags)
            http://www.java-forums.org/advanced-java/45591-timezone-confusion.html

            db
            • 3. Re: TimeZone is off by one hour
              798692
              867502 wrote:
              Though it should be printing out:

              Pacific Daylight Time

              What am I doing wrong here?
              The method getDisplayName() can also have the boolean argument for daylight saving name. If you want the above expected result, then you should use
              getDisplayName(true,1)
              For more information related with the TimeZone, Refer Here
              • 4. Re: TimeZone is off by one hour
                EJP
                You could at least have posted a link to the current documentation. JDK 1.4 is eight years old!
                • 5. Re: TimeZone is off by one hour
                  798692
                  EJP wrote:
                  You could at least have posted a link to the current documentation. JDK 1.4 is eight years old!
                  Oh i didn't notice that. I will correct this here after.
                  • 6. Re: TimeZone is off by one hour
                    870505
                    Thanks. I was sorta of expecting getDisplayName to default and return the current TimeZone in a given location. When I use the TimeZone.inDaylightTime(Date ofNow) to check if the timezone is in DST, I get the expected result with getDisplayName.

                    Here is the updated working code

                    import java.util.Date;
                    import java.util.TimeZone;
                    public class TimeZoneTest {
                         public static void main(String[] args) {
                              TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
                              boolean retVal = tz.inDaylightTime(new Date());
                              System.out.println(tz.getDisplayName(retVal, 1));
                         }
                    }
                    • 7. Re: TimeZone is off by one hour
                      870505
                      I didn't realize I had to format 8 lines of code to get help here. My apologies. I will format the code perfectly next time. And sorry for posting to a thread that was completely related to my question. I will start a new thread next time.

                      Edited by: 867502 on Jun 22, 2011 6:55 AM
                      • 8. Re: TimeZone is off by one hour
                        jschellSomeoneStoleMyAlias
                        867502 wrote:
                        I was sorta of expecting getDisplayName to default and return the current TimeZone in a given location.
                        You are using a default time zone. You are getting a specific one. The code isn't going to guess what you want.

                        As an example of that you can look to any windows box and note that the timezone setting is based on the timezone AND has a check box to set for DST.

                        The reason for this is that there are places in the US (and probably the rest of the world) where localities do not use DST,
                        • 9. Re: TimeZone is off by one hour
                          darke
                          867502 wrote:
                          I didn't realize I had to format 8 lines of code to get help here.

                          Edited by: 867502 on Jun 22, 2011 6:55 AM
                          But you did format the code for the other forum , didnt you .
                          • 10. Re: TimeZone is off by one hour
                            EJP
                            I didn't realize I had to format 8 lines of code to get help here.
                            It's advisable. Regular respondents are far more likely to respond helpfully if they can actually read the code. Same reason you format it for yourself in the IDE.