4 Replies Latest reply: Mar 31, 2010 12:34 AM by Murray9654 RSS

    problem with calender timezone

    Murray9654
      hi first i got the instance of the calendar and printed the date? The date showed in IST which is my timezone. After this i changed the Timzone of the calendar to "UTC" and printed the date. still the date comes in IST and the date is same before and after changing the timezone. how to understand this? why is the time not changing according to the timezone set? why the time is always showing in IST which is my OS timezone?
        • 1. Re: problem with calender timezone
          DrClap
          Date objects don't have timezones. That's how to understand it. If you want to display a Date in a particular timezone then create a SimpleDateFormat and apply that particular timezone to it, then use its format() method to display the Date.
          • 2. Re: problem with calender timezone
            Murray9654
            DrClap wrote:
            Date objects don't have timezones. That's how to understand it. If you want to display a Date in a particular timezone then create a SimpleDateFormat and apply that particular timezone to it, then use its format() method to display the Date.
            hi thank you. Then i am confused with the api. please see the following.
            the following is the api
            java.util.Calendar.setTimeZone(TimeZone value)
            this mean that we can set the timezone to the calender. am i right?

            if date object does not contain timezone information when i printed it it clearly prints IST . how is it able to that?

            Suppose i have date like 22-03-2009 12:30:30
            when i use dateformat with a particular timezone info how will it convert the date exactly to that time zone when it don't know the timezone of the given date.

            Edited by: murrayb9654 on Mar 27, 2010 8:35 PM
            • 3. Re: problem with calender timezone
              DrClap
              murrayb9654 wrote:
              DrClap wrote:
              Date objects don't have timezones. That's how to understand it. If you want to display a Date in a particular timezone then create a SimpleDateFormat and apply that particular timezone to it, then use its format() method to display the Date.
              hi thank you. Then i am confused with the api. please see the following.
              the following is the api
              java.util.Calendar.setTimeZone(TimeZone value)
              this mean that we can set the timezone to the calender. am i right?
              Yes.
              if date object does not contain timezone information when i printed it it clearly prints IST . how is it able to that?
              You printed a string representation of the date which interpreted the date as IST. It's able to do that by computer programming. And after all it has to represent the date in some time zone -- which time zone should it choose if not your system's default time zone?
              Suppose i have date like 22-03-2009 12:30:30
              That isn't a date. It could represent a variety of dates, depending on what time zone you use to interpret it.
              when i use dateformat with a particular timezone info how will it convert the date exactly to that time zone when it don't know the timezone of the given date.
              You aren't paying attention. A Date doesn't have a timezone. It is simply the number of milliseconds since a particular instant of time. That instant of time (I'm recapping the API documentation which you haven't read here) was midnight on 1 January 1970 in UTC. And the SimpleDateFormat object represents that instant, or any number of milliseconds after it, using computer programming. Note that the SimpleDateFormat object will represent that instant differently depending on what timezone you assign to the SimpleDateFormat object. You could write some code to try that yourself.
              • 4. Re: problem with calender timezone
                Murray9654
                I completely agree with the above explanation. I want to add some points in addition.

                no matter what the timezone of the DateFormat is. if you create a date object using Dateformat or any means the date will always be represented in system timezone but you can format the Date to different timezones using DateFormat. Thank you so much.