×
Namespaces

Variants
Actions
(Difference between revisions)

Converting time zones in Java ME

From Nokia Developer Wiki
Jump to: navigation, search
Forum Nokia KB (Talk | contribs)
Forum Nokia KB (Talk | contribs)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
 
__NOEDITSECTION__
 
__NOEDITSECTION__
 +
{{KBCS}}
 
{{CodeSnippet
 
{{CodeSnippet
|id= 
+
|id=CS001199
 
|platform=S60 3rd Edition, S60 3rd Edition, FP1, S60 3rd Edition, FP2
 
|platform=S60 3rd Edition, S60 3rd Edition, FP1, S60 3rd Edition, FP2
 
|devices=Nokia E70, Nokia N78
 
|devices=Nokia E70, Nokia N78
 
|category=Java ME
 
|category=Java ME
 
|subcategory=Date/time/clock
 
|subcategory=Date/time/clock
|creationdate=October 28, 2008
+
|creationdate=December 9, 2008
 
|keywords=java.util.TimeZone, java.util.TimeZone.getAvailableIDs, java.util.TimeZone.getRawOffset
 
|keywords=java.util.TimeZone, java.util.TimeZone.getAvailableIDs, java.util.TimeZone.getRawOffset
 
}}
 
}}
Line 15: Line 16:
 
This code snippet demonstrates how to convert time zones.  
 
This code snippet demonstrates how to convert time zones.  
  
List of all supported time zones is shown on screen. By choosing time zone from list, user can see what time offset does it have.
+
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 <tt>TimeZone.getTimeZone()</tt> allows application to retrieve chosen Time Zone, method <tt>TimeZone.getRawOffset()</tt> used to retrieve raw time offset for chosen time zone.
+
Method <tt>TimeZone.getTimeZone()</tt> allows the application to retrieve the chosen Time Zone and method <tt>TimeZone.getRawOffset()</tt> is used to retrieve the raw time offset for the chosen time zone.
  
 
==Source file: ConvertingTimeZones.java==
 
==Source file: ConvertingTimeZones.java==
Line 120: Line 121:
 
==Postconditions==
 
==Postconditions==
  
This application allows user to retrieve time offset for any time zone he needs.
+
This application allows the user to retrieve the time offset for any time zone he or she needs.
  
 
==Supplementary material==
 
==Supplementary material==
  
You can see source file and executable application in attached zip archive. Archive is available for download at [[Media:Converting_time_zones_in_J2ME.zip]]
+
The source file and executable application are available for download at [[Media:Converting_time_zones_in_J2ME.zip]].
  
 
[[Category:Java ME]][[Category:Code Examples]]
 
[[Category:Java ME]][[Category:Code Examples]]

Revision as of 14:29, 9 December 2008


Template:KBCS

Article Metadata
Tested with
Devices(s): Nokia E70, Nokia N78
CompatibilityArticle
Keywords: java.util.TimeZone, java.util.TimeZone.getAvailableIDs, java.util.TimeZone.getRawOffset
Created: (09 Dec 2008)
Last edited: Forum Nokia KB (09 Dec 2008)

Overview

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");
form.addCommand(backCommand);
form.setCommandListener(this);
 
//Create list filled with all available time zone IDs.
list = new List("Select time zone:", List.IMPLICIT,
timeZone.getAvailableIDs(), null);
list.addCommand(exitCommand);
list.setCommandListener(this);
 
display = Display.getDisplay(this);
display.setCurrent(list);
}
 
/**
* 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.
form.deleteAll();
//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.
form.append(String.valueOf(
timeZone.getTimeZone(timeZoneID).getRawOffset() / 3600000));
form.append(" hours");
 
display.setCurrent(form);
} else if (cmd == backCommand) {
display.setCurrent(list);
} else if (cmd == exitCommand) {
notifyDestroyed();
}
}
 
/**
* 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) {
}
}

Postconditions

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.

91 page views in the last 30 days.