File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Converting date to different timezones

 
Murali Pen
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used the following code to convert an input date string to different timezones. However, this code is working only if the input date string is in TimeZone GMT and fails to convert when it is in any other timezone. Can someone throw any light?
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class java.util.Date contains the number of milliseconds since the epoch of 01/01/1970 00:00:00.000 UTC. It contains no explicit time zone information but has an implicit time one of UTC ( effectively GMT ) . This applies no matter what time zone your computer is configured to.

When you parse a string using SimpleDateFormat to get a java.util.Date a time zone is needed. If one does not explicitly specify a time zone then the time zone of your computer is used.

When one formats a java.util.Date to a String using SimpleDateFormat a time zone is needed. If one does not explicitly specify a time zone then the time zone of your computer is used.





 
Murali Pen
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But my code is explicitly setting the time zone to SimpleDateFormat in getDateInTimeZone() method. Can someone explain why this works only when GMT date string is used as input?
 
Paul Clapham
Sheriff
Pie
Posts: 20177
25
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your method, if it is to do what you claim it does, should take two time zones as parameters. The "from" time zone and the "to" time zone. As it is, you convert a string to a date using time zone X, and then convert the date back to a string using that same time zone. This basically does nothing.

Actually, I notice you used the phrase "failed to convert" in your original post. What happened instead?
 
Pat Farrell
Rancher
Posts: 4660
5
Linux Mac OS X VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One critical thing to understand is that in Java, a Date object does *NOT* have a timezone. Nothing you can do to a Date will change its timezone.

Only DateFormats have timezones. So you set the timezone you want into the DateFormat (or any class that subclasses from it). Use the DateFormat to output your Date and you will be happy.
 
Paul Clapham
Sheriff
Pie
Posts: 20177
25
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it's significant that the input date already specifies a time zone. When I ran this code:

the output looked like this:
Mon Mar 14 02:30:51 PDT 2011
Mon Mar 14 05:30:51 EDT 2011

I didn't find that surprising, in fact it looks perfectly reasonable to me. So my question is still, what are you expecting?
 
Murali Pen
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed my method to following and it works. Thanks everyone for all the suggestions.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic