1 Reply Latest reply: Apr 10, 2009 2:49 PM by 807588 RSS

    How to set Timezone in a date object

    807588
      Hi,

      I need to write a method setTimeZone(Date d,TimeZone t) which should take a Java.util.Date and a Timezone object and set the time zone to the date object.


      for example...


      Date d=new Date();


      TimeZone tz=TimeZone.getTimeZone("PST");


      //should print the timezone as EDT because the current system timezone is EDT


      System.out.println(d);


      setTimeZone(d,tz);


      //should print the timezone as PST


      System.out.println(d);



      public void setTimeZone(Date d,TimeZone tz){


      Calendar c=Calendar.getInstance(tz);


      c.setTime(d);


      c.setTimeZone(tz);


      d=c.getTime();



      System.out.println("Timezone in calendar is "+c.getTimeZone().getDisplayName());


      }


      But today is still showing up EDT timezone and Timezone in calendar is shown as PST.The new timzone is not aplied to date object @ d=c.getTime();


      statement.


      Could any one please help me...

      Edited by: mchepuri on Apr 10, 2009 7:27 PM

      Edited by: mchepuri on Apr 10, 2009 7:29 PM