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

Get time zone offsets with DST

by Retired ‎02-16-2010 03:01 PM - edited ‎09-16-2010 03:02 PM (5,468 Views)



This article applies to the following:

  • BlackBerry® smartphones based on Java® technology



When working with time zones it is often necessary to obtain the Greenwich Mean Time (GMT), offset in the particular region. In most cases you can simply use the java.util.TimeZone.getDefault which returns the default time zone configured on your BlackBerry smartphone.


If you need to programmatically obtain the GMT offset for your particular time zone you can use one of the following two methods:


1. java.util.Timezone.getRawOffset returns the GMT offset for the particular Timezone object.


Note: This method does not take Daylight Savings Time (DST) into consideration.


2. java.util.Timezone.getOffset gets the GMT offset for the given date, modified in the case of DST. This is the offset to add to GMT to get the local time. Also, this method requires the date and time to be passed as arguments. You can see an example of this below.


Note: getRawOffset and getOffset return the time difference from GMT in milliseconds.

If you wish to convert to hours you must perform the following calculation:


Eastern Standard Time - GMT Offset of -18,000,000 milliseconds


(1000 milliseconds * 60 seconds * 60 minutes = 3,600,000)


Therefore, -18,000,000/3,600,000 = -5 hours.


Note: In the case of DST, -14,400,000/3,600,000 = -4 hours


The following is an example using the offset methods in a Java application:



import java.util.*;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;

public class Main extends UiApplication {

private static Main myApp;

public Main() {
MyScreen oMyScreen = new MyScreen();

public static void main(String[] args) {
myApp = new Main();

private class MyScreen extends MainScreen {

public MyScreen() {
//Arguments for the getOffset method.
//era - The era of the given date (0 = BC, 1 = AD)
//year - The year in the given date.
//month - The month in the given date. Month is 0-
//based. e.g., 0 for January.day - The day-in-month
//of the given date. dayOfWeek - The day-of-week of
//the given date. millis - The milliseconds in day //in standard local time.

Calendar rightnow =

add(new LabelField("year: "
+ rightnow.get(Calendar.YEAR)));
add(new LabelField("month: "
+ rightnow.get(Calendar.MONTH)));
add(new LabelField("day of month: "
+ rightnow.get(Calendar.DAY_OF_MONTH)));
add(new LabelField("day of week: "
+ rightnow.get(Calendar.DAY_OF_WEEK)));
add(new LabelField("millis: "
+ rightnow.get(Calendar.MILLISECOND)));

int offsetDST = 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));

add(new LabelField("Offset (Daylight Savings): "
+ offsetDST));
add(new LabelField
("Offset (Without Daylight Savings): "
+ TimeZone.getDefault().getRawOffset()));




Users Online
Currently online: 36 members 2,841 guests
Please welcome our newest community members: