aspose file tools*
The moose likes Java in General and the fly likes Convert Date from one timezone to another Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Convert Date from one timezone to another" Watch "Convert Date from one timezone to another" New topic
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: 3274
    
    5

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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14338
    
  22

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 8 API documentation
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: 18886
    
    8

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: 18886
    
    8

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 [ ]
Venkateswara Rao Desu
Greenhorn

Joined: Apr 13, 2009
Posts: 7
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
}




-- Venkateswara Rao Desu
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14338
    
  22

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
Bartender

Joined: Oct 14, 2005
Posts: 18886
    
    8

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

Joined: Dec 19, 2013
Posts: 2
Jasper suggestion on using SimpleDateFormat is the best. But, below is some alternate way as well using JodaTime.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Convert Date from one timezone to another