(Difference between revisions)

Converting time zones in Java ME

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot update - Fix links)
Line 1: Line 1:
{{ArticleMetaData <!-- v1.2 -->
{{ArticleMetaData <!-- v1.2 -->
|sourcecode= [[Media:Converting time zones in J2ME.zip]]  
|sourcecode= [[Media:Converting time zones in J2ME.zip]]
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
|devices= Nokia E70, Nokia N78
|devices= Nokia E70, Nokia N78

Revision as of 03:20, 13 July 2012

Article Metadata
Code ExampleTested with
Devices(s): Nokia E70, Nokia N78
Keywords: java.util.TimeZone, java.util.TimeZone.getAvailableIDs, java.util.TimeZone.getRawOffset
Created: vltsoy (27 Nov 2008)
Last edited: hamishwillee (13 Jul 2012)



This code snippet demonstrates how to convert time zones.

A list of all supported time zones is shown on the screen. By choosing a time zone from the list, the user can see the time offset it has. Method TimeZone.getTimeZone() allows the application to retrieve the chosen Time Zone and method TimeZone.getRawOffset() is used to retrieve the raw time offset for the chosen time zone.

Source file: ConvertingTimeZones.java

import java.util.TimeZone;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
public class ConvertingTimeZones extends MIDlet implements CommandListener {
private List list;
private Form form;
private Display display;
private TimeZone timeZone;
private Command backCommand;
private Command exitCommand;
* Constructor. Constructs the object and initializes buffer.

public ConvertingTimeZones() {
timeZone = TimeZone.getDefault();
backCommand = new Command("Back", Command.BACK, 1);
exitCommand = new Command("Exit", Command.EXIT, 0);
form = new Form("Time zone info");
//Create list filled with all available time zone IDs.
list = new List("Select time zone:", List.IMPLICIT,
timeZone.getAvailableIDs(), null);
display = Display.getDisplay(this);
* From CommandListener.
* Called by the system to indicate that a command has been invoked on a
* particular displayable.
* @param cmd the command that was invoked
* @param displayable the displayable where the command was invoked

public void commandAction(Command cmd, Displayable displayable) {
if (cmd == List.SELECT_COMMAND) {
//Clear form.
//Get selected time zone index.
String timeZoneID = list.getString(list.getSelectedIndex());
form.append("Time zone ID: " + timeZoneID + "\n");
form.append("Time offset: ");
//Display the GMT offset for selected time zone in hours.
timeZone.getTimeZone(timeZoneID).getRawOffset() / 3600000));
form.append(" hours");
} else if (cmd == backCommand) {
} else if (cmd == exitCommand) {
* From MIDlet.
* Called when the MIDlet is started.

public void startApp() {
// No implementation required.
* From MIDlet.
* Called to signal the MIDlet to enter the Paused state.

public void pauseApp() {
// No implementation required.
* From MIDlet.
* Called to signal the MIDlet to terminate.
* @param unconditional whether the MIDlet has to be unconditionally
* terminated

public void destroyApp(boolean unconditional) {


This application allows the user to retrieve the time offset for any time zone he or she needs.

Supplementary material

The source file and executable application are available for download at Media:Converting time zones in J2ME.zip.

159 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.