File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes TimeZone 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 Conversion" Watch "TimeZone Conversion" New topic
Author

TimeZone Conversion

Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
Hi Guys,

I was messing around with the Facebook API. Basiclly i created a calendar event on facebook starting at 19 Nov, 14:15 and end 15:15, then used the api to get the event as XML.

Now the event start date appeared as:

<start_time>1227042900</start_time>
<end_time>1227046500</end_time>

The 10-digit number represents the number of seconds since 1970 accurate down to the second, and is the most common way to express timestamp values, The java Date constructor as far as i know takes a 13-digit number accurate down to the millisecond to work correctly and create a Date. So i multipled the xml timestamp number by 1000 to make it accurate down to millisecond and created the start date in java using the following code.


Which later displayed as 19 Nov 22:15 which was a discremency of 8 hours.

According to the facebook forum they think Since there's no timezone specified when you enter an event, it seems facebook stores it internally as US Pacific Standard Time (which is GMT-8). When it is exported as a timestamp, it gets "normalized" to standard timestamp conventions by expressing it as GMT. In other words when i enter 14:15, facebook will treat this as 14:15 USPST, which equals 22:15 GMT when converted above.

So i wonder can any body show me how to read the above xml timestamp result into java so it can be converted into the timezone of the user who created it. Facebook also stores the timezone of the user who queried the facebook server. My time zone appears as 0, So i wonder how can i in java give the facebook server time zone and event time start time, compare them to my time zone, get the discrmency and deduct as neccessary? Any help appreciated.

best
Mark Hughes

[ November 19, 2008: Message edited by: Mark Hughes ]
[ November 19, 2008: Message edited by: Mark Hughes ]
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Hi there,

A little bit of a tough one alright! Unfortunately in terms of creating a Date in Java you can only seed it with the 13 digit number that is time since 1970 in GMT.

You can however then create a Calendar object out of that and then set the timezone/locale (that you get separately from Facebook) on that Calendar object. See the Calendar and Gregorian calendar API.


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
Hi Martijn ,

Thanks for your reply, yes i looked into what you suggested, now im thinking that even if i was just to create the formatted time to appear as the PST GMT-8 time than that would appear as the time entered when the event was created and would be correct. So i tried this code, basically create calendar object with GMT-8 timezone and hope that when i pass in the facebook time stamp it appears as the original time i enter when creating the event.



Now the event i created is as follows;
Date:19 November 2008
Time:16:00 - 17:00

But the above code result is "Correct Time:2008-11-20 01:00" which is really off, any thoughts??

Best
Mark
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39815
    
  28
Depends where you are. If you are in Western North America, then by the time 4pm 19th November comes, it will be after midnight here (UK), so 1am 20th November looks right. You might need to put some sort of timezone correction in.
Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
Hi Campbell ,

Yup im based in Ireland, and i create the event intending to have it in Ireland, so id like to pull the event out and display it as the time i entered and not 01:00 in the morning. I finding it a bit tricky to say the least lol

Best
Mark
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18887
    
    8

You might find it helpful to set the time zone on your SimpleDateFormat as well. I'm not sure that will work but that's just because I didn't spend the time following all of the conversions there.

By the way "Etc/GMT-8" is almost certainly not the right timezone to use. It may work OK now but come March of next year you may find it to be an hour off. Try "America/Los_Angeles" instead.
Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
Hey Guys,

Yup thanks Paul i tried what you suggested and changed the time zone to America/LA and also set the time on the SimpleDateFormat as follows:



And then i got the time as expected, i think doing it like this will always give the correct Times back as you pull them out in the same format as they were entered.

Thansk to everyone for all the help and feed back

Best
Mark
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39815
    
  28
Not sure how much I actually helped, but well done

And it is useful to leave the solution there in case anybody needs to search for it in future.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: TimeZone Conversion