| Author |
Convert Date from one timezone to another
|
saumil baxi
Ranch Hand
Joined: Apr 18, 2008
Posts: 58
|
|
Hi, I am stuck at one place. I get the date , time and the timezone of the user. I want to convert that date and time into my System timezone . Thanks, Saumil
|
 |
Martijn Verburg
author
Bartender
Joined: Jun 24, 2003
Posts: 3268
|
|
Have you had a look at the Calendar, Date and Timezone Javadocs? Also search this forum as this question has been answered many times before
|
Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
|
 |
saumil baxi
Ranch Hand
Joined: Apr 18, 2008
Posts: 58
|
|
Below is the code that i worte to convert from one timestamp to another I am getting timeZone and date as an input But i am getting Parsing Exception java.text.ParseException: Unparseable date: "22 Jan 3909 00:00:00 Asia/Hong_Kong"
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12907
|
|
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 ]
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
saumil baxi
Ranch Hand
Joined: Apr 18, 2008
Posts: 58
|
|
Thanks for the reply. But my requirement is bit different. I will get Date and the Timezone as an input and I need to convert that date into Some other TimeZone. So the function that I am looking out is something like This.. ConvertDate(Date date,TimeZone Original,TimeZone Convert) { This function should return me the date and time belonging to Timezone COnvert }
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16482
|
|
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.
|
 |
saumil baxi
Ranch Hand
Joined: Apr 18, 2008
Posts: 58
|
|
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. So now I have the date and the TimeZone. For Quartz i have to convert the date and time that the user have provided from his timezone to corresponding date and time of the server's timezone.
I hope its clear that its not a bogus requirement . [ December 10, 2008: Message edited by: saumil baxi ]
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16482
|
|
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:
|
 |
saumil baxi
Ranch Hand
Joined: Apr 18, 2008
Posts: 58
|
|
Thanks, Job done [ ]
|
 |
 |
|
|
subject: Convert Date from one timezone to another
|
|
|