First, you have to understand the following: Class Date does not know anything about timezones. A Date object does not have a timezone. So, you cannot convert a Date object into a specific timezone, because the Date object doesn't know anything about timezones.
When you format a Date object into a string using a DateFormat object, then you can set the timezone on the DateFormat object, to tell it in which timezone to display the Date. For example:
[ December 04, 2008: Message edited by: Jesper Young ]
You can say that's your requirement as much as you want, but it doesn't make any sense. As Jesper Young said, a Date doesn't have a timezone. So you can't do anything to a Date to make it have a different timezone, because it didn't have a timezone in the first place.
It's possible to format a Date object to show what time it represents in a particular timezone by using a SimpleDateFormat object whose TimeZone property has been set to that particular timezone.
Now it's possible that you have some similar requirement but that you haven't phrased it meaningfully. For example, it's possible that you got a Date object from some other process (such as a database) which was produced assuming a different timezone than yours, and that the Date was transferred from that system without adjusting for that assumption. I encountered that once when the program putting the timestamps into the database was a .Net program, which stores timestamps uniformly using GMT instead of using the database's timezone. For that I had to write some code to correct the discrepancy.
But it's also possible that you just don't understand how Dates work. So it's necessary to get the real requirement before we try to implement bogus requirements.
Joined: Apr 18, 2008
I Understand that Date Object doesnt have a TimeZone
Here is the complete scenario and how I am doing it .
I take date and time input from the user and form a Date object.
I Get the user TimeZone from Internal User object.
Ah, I see. So your requirements are actually this: You have a String that the user entered. (Not a Date.) This String represents a timestamp in a certain timezone, and you get a TimeZone object which represents that timezone. And you want to create a Date object from that. So:
I'm sorry to say that your code is flawed, because it's based on an incorrect idea of what a java.util.Date object really represents.
A java.util.Date object does not have timezone information by itself. It represents an "absolute" moment in time. (Internally, it contains a number of milliseconds since a fixed point in time - 01-01-1970, 00:00:00 UTC).
What your code does is shift the time that your input Date object has by a number of hours (the difference between the two timezones you pass in). But that's not how you should use Date objects.
What you should do if you want to print the date in a different timezone, is use a DateFormat object and set the timezone on the DateFormat object to indicate in what timezone you want the date displayed. For example:
Jesper de Jong wrote:What your code does is shift the time that your input Date object has by a number of hours (the difference between the two timezones you pass in).
And to be more specific, that is wrong because the difference between the two timezones is not necessarily constant. It can vary through the year as daylight saving time starts and ends in the two time zones. So for example the difference between the timezones used in western North America and the timezones used in southeastern Australia can be 17, 18, or 19 hours depending on whether it is daylight saving time in neither, one, or both of the timezones.