This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert Date from one timezone to another

 
saumil baxi
Ranch Hand
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
saumil baxi
Ranch Hand
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
saumil baxi
Ranch Hand
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20177
25
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20177
25
MySQL Database
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,

Job done [ ]
 
Venkateswara Rao Desu
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saumil baxi wrote: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
}


 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Venkateswara.

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:

 
Paul Clapham
Sheriff
Pie
Posts: 20177
25
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gubendran Lakshmanan
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jasper suggestion on using SimpleDateFormat is the best. But, below is some alternate way as well using JodaTime.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic