Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Contributor
BB_Learn_AK
Posts: 7
Registered: ‎08-12-2011
My Device: BB 8900

How to get GMT time zone offset

How to get GMT based  time zone offset?

 

SimpleDataFormat class supports 'z' and returns strings like "America/Chicago".

 

But, I would like to get the offset time from GMT.(Example GMT: -8:00)

 

Other platforms support 'Z' option which returns the offset.

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: How to get GMT time zone offset

http://www.blackberry.com/developers/docs/6.0.0api/java/util/TimeZone.html#getRawOffset()
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to get GMT time zone offset

It might be a bit more complicated than that because I don't believe rawOffset includes DST. 

 

I don't think there is an easy way to do this, but I would love to be proved wrong.

 

If there is no easy way, then I would determine the difference as follows:

a) Create a Calendar using GMT

b) Create a Calendar using local time

c) set them both to the same time (note that this does have to be the date/time that you are planning to display.  You can't set the current date/time and expect the difference to be the same because it might be DST now and NOT at the time you are going to display)

d) Extract the hours and minutes from the Calendar Object to determine the difference.  This might be quite complicated because of the possible 'end cases', e.g., if you are +8 hours, then the hour 23 from the GMT Calendar will correspond top hour 7 from the local time and you don't want to interpret this as plus 16. 

New Developer
CobiRic
Posts: 21
Registered: ‎03-31-2011
My Device: Developer

Re: How to get GMT time zone offset

I found another answer at: Get time zone offsets with DST

 

The basic idea is to look at Calendar.getOffset(). The trick is that this method takes a lot of arguments, so first create a time "right now".

 

Code extract below:

           Calendar rightnow =
Calendar.getInstance(TimeZone.getDefault());

int timezoneOffset = TimeZone.getDefault().getOffset(1, rightnow.get(Calendar.YEAR), rightnow.get(Calendar.MONTH), rightnow.get(Calendar.DAY_OF_MONTH), rightnow.get(Calendar.DAY_OF_WEEK), rightnow.get(Calendar.MILLISECOND));
www.cobiinteractive.com
www.madmob.co.za