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

Time Zone Issue

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a TimeZone problem.

I have a dete in IST format say 3:24:58 PM IST 2008

I need the corresponding London time, I used the formatter to do it for me and what i get is 10:55:44 AM IST 2008. Why does it come in IST? I need the output as 10:55:44 AM BST 2008. Here is the code that I used to convert the Indian Time to equal London Time.

/* The one that gives corresponding london time*/


/* The London Date Formatter*/


Any help on this ASAP would be nice!!
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice! Wondering if this is solved yet.
 
Saloon Keeper
Posts: 15485
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It appears nobody answered Karthick's question in 14 years. Better late than never:

A Date represents a fixed point in time, and one should not consider it as a certain time in a certain timezone.

So while the string "3:24:58 PM IST 2008-09-07" is different from the string "10:54:58 AM BST 2008-09-07", when parsed both will yield equal points in time, aka Date.

That necessarily means that you can't convert a Date to another timezone, because as we've seen, a fixed point in time is not associated with just one specific time zone.

Now, it's true that the Date class contains a method getTimezoneOffset() which implies that an instance of Date keeps track of at least some timezone information, but you must ban this thought from your head. The Date class does NOT represent looking at a clock in some timezone. The Java designers have realized this, and have deprecated almost every method of Date 25 years ago.

Karthick's solution should have looked something like this:


While the solution above works, it's dated. As of Java 8, you should be using the java.time packages instead:
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:. . . While the solution above works, it's dated. . . .

It would have been up to date fourteen years ago Maybe he would have got a faster answer if he hadn't said, “ASAP.”

There is a similar, but more complicated, example in the Java™ Tutorials (look for “ZonedDateTime”).
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arfaz Hussain wrote:Nice! Wondering if this is solved yet.



Note: The java.util.Date object is defined as containing the number of milliseconds elapse since Midnight, Jan 1, 1970 Universal Time. That means that it does in fact include a time zone.

Now often we play fast and loose with Dates and treat them as local times, but that's hazardous, because if you use the no-argument Date constructor, the resulting object will return with the current date/time in UTC.

JVMs have historically had mechanisms that favored conversions to the local — that is Locale — timezone as a convenience, but it's not an especially difficult task to code for conversions to some other zone(s).

reply
    Bookmark Topic Watch Topic
  • New Topic