• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Converting date to different timezones

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 28322
95
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
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?
 
Rancher
Posts: 4804
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 28322
95
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
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I changed my method to following and it works. Thanks everyone for all the suggestions.
 
machines help you to do more, but experience less. Experience this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic