my dog learned polymorphism*
The moose likes Java in General and the fly likes Timezone offset to Timezone ID conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Timezone offset to Timezone ID conversion" Watch "Timezone offset to Timezone ID conversion" New topic
Author

Timezone offset to Timezone ID conversion

Roshni Paul
Greenhorn

Joined: Mar 24, 2006
Posts: 13
Hi,

I was searching for one function that convert the timezone offset value in milliseconds to the corresponding TimeZone String ID representation.

One more doubt is that, my client is sending me time in milliseconds, if I creat a Date object with this time will it represent the clients TimeZone or will it be the TimeZone of my machine where the Date object is created.

Also, My Time Zone is "IST". It is 5:30 hrs ahead of UTC. But when I created one Date object and called getTimeZoneOffset() function on that what i am getting is -330. I am not able to understand why it is coming as negative even when the time is ahead of UTC.

I would greatly appreciate if somebody can help me out.

Thanks in advance,
Martin
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18993
    
    8

Originally posted by Roshni Paul:
One more doubt is that, my client is sending me time in milliseconds, if I creat a Date object with this time will it represent the clients TimeZone or will it be the TimeZone of my machine where the Date object is created.
No. Neither. A Date object does not represent any time zone. It is an absolute number, the number of milliseconds since midnight GMT on January 1, 1970. You can format it for any time zone; for example the same Date object represents 2006 March 24 11:00:00 UTC and 2006 March 24 16:30:00 IST.
Also, My Time Zone is "IST". It is 5:30 hrs ahead of UTC. But when I created one Date object and called getTimeZoneOffset() function on that what i am getting is -330. I am not able to understand why it is coming as negative even when the time is ahead of UTC.
Sorry, I don't see a method of that name in any class in the standard Java API. If I create a TimeZone for "IST" and then call its getOffset(long) method, it returns 19800000 -- a positive number which is the number of milliseconds in 5.5 hours. You'll have to ask the writer of that method about it.
Roshni Paul
Greenhorn

Joined: Mar 24, 2006
Posts: 13
Hi Paul,

Thankyou very much for your reply. I very much agree with you that Date is an absolute number, the number of milliseconds since midnight GMT on January 1, 1970. But if it does not contain timezone information how can be the same absolute value be presented as different times values for different time zones. Could you please put little more light on how this actually works.

Could you please suggest some function that mapps the offset to the time zone string id?

Also,

Date d=new Date();
long m=d.getTimezoneOffset();
System.out.println(m);

This is the getTimezoneOffset() function that I mentioned. It is from java API itself. The out put I am getting is -330.

Thanks,
Rosh
Dan Patterson
Greenhorn

Joined: Jan 27, 2005
Posts: 24
You should look at the Calendar class, it looks like it has everything you are looking for. Dates getTimezoneOffset() has been deprecated since JDK 1.1.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18993
    
    8

Notice that the Date object is not doing this presentation. It just contains a number.
Could you please suggest some function that mapps the offset to the time zone string id?
You want to know what timezone is UTC - 6 hours for example? There is no such function because it is possible there are several timezones with that offset. But TimeZone.getAvailableIDs(offset) will return you an array of them. (And its API documentation explains why it must return an array.)
[ March 27, 2006: Message edited by: Paul Clapham ]
Roshni Paul
Greenhorn

Joined: Mar 24, 2006
Posts: 13
Hi Paul,

Thank you very much. getAvailableIDs(offset) was exactly the function that was searching for.

Thanks,
Rosh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Timezone offset to Timezone ID conversion