aspose file tools*
The moose likes Java in General and the fly likes Converting date to different timezones Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Converting date to different timezones" Watch "Converting date to different timezones" New topic
Author

Converting date to different timezones

Murali Pen
Greenhorn

Joined: May 23, 2006
Posts: 28
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?


Murali
SCJP,SCBCD,SCWCD
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

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.







Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Murali Pen
Greenhorn

Joined: May 23, 2006
Posts: 28
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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Aug 11, 2007
Posts: 4655
    
    5

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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: May 23, 2006
Posts: 28
I changed my method to following and it works. Thanks everyone for all the suggestions.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Converting date to different timezones