Win a copy of Fixing your Scrum this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Convert Date from one timezone to another

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 3281
8
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
}
 
Marshal
Posts: 26747
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 26747
81
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks,

Job done [ ]
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 26747
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jasper suggestion on using SimpleDateFormat is the best. But, below is some alternate way as well using JodaTime.
 
Creativity is allowing yourself to make mistakes; art is knowing which ones to keep. Keep this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic