I have been working on timezones and timezone conversions for the past two days and havent been able to understand some of the fundamentals. Some of these may be a bit trivial!
1. I have read that Calendar object is Time zone dependent and Date object isnt? I am not really convinced with it. What is the reality?
2. Is time in millis since epoch, Time zone dependent? (Both calendar's getTimeInMillis() and date's getTime()).
3. In whatever Time zone I have my Calendar object in, when I use the getTime() method to get a Date object, the Date object displays time in the system default time zone.
4. I need to have a method which can take time in millis in GMT and return me a GMT Calendar/Date, irrespective of what timezone the program is running in and vice versa. How do I go about it? My tries have led to a lot of Time Zone conversions to my local TimeZone.
A lot of your answers could have easily been answered by reading the API documentation. For example, Date's getTime() method is described like this:
Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.
That answers #2 directly, it's based on a specific time zone. And the answer for #3 follows directly from that. But you'll observe that Calendar has getTimeZone() and setTimeZone() method, which strongly suggests that a Calendar has a time zone attribute.
I don't know what you have tried. But for your description I would just do this:Also, if you want to format a Date, or a Calendar, in a specific time zone then note that SimpleDateFormat also has a setTimeZone() method; it will convert timestamps to your local time zone if you don't tell it otherwise. And yes, you should be using SimpleDateFormat to format timestamps instead of relying on the toString() method of Date and Calendar.
posted 14 years ago
I am still a bit unclear about my first question. Is Date Time-Zone dependent? I am not quite able to understand the behaviour of this piece of code.
OUTPUT : Calendar stamp -> 2006-07-14 16:12:06.916 GMT Date stamp -> Fri Jul 14 21:42:06 IST 2006
How is my Date object auto-converted to the default TimeZone?