Java Reference
In-Depth Information
The use of the system property in this example sets the default time zone to “Asia/
Jakarta” before running the
Auctioneer
class. This affects any
Date
objects in a Java
program that do not set their own zone.
These property changes are not permanent; they only apply to that particular execution of
the class and any classes that it uses.
In the
java.util
package, the
TimeZone
class includes a class
method called
getProperties()
that returns a string array contain-
ing all the time zone identifiers that Java supports.
The following code displays these identifiers:
String[] ids = java.util.TimeZone.
➥
getAvailableIDs();
for (int i = 0; i < ids.length; i++) {
System.out.println(ids[i]);
TIP
}
You also can create your own properties and read them using the
getProperty()
method
of the
System
class, which is part of the
java.lang
package.
Listing B.4 contains the source code of a simple program that displays the value of a
user-created property.
LISTING B.4
The Full Text of
ItemProp.java
1: class ItemProp {
2: public static void main(String[] arguments) {
3: String n = System.getProperty(“item.name”);
4: System.out.println(“The item is named “ + n);
5: }
6: }
If this program is run without setting the
item.name
property on the command line, the
output is the following:
The item is named null
The
item.name
property can be set using the
-D
option, as in this command:
java -Ditem.name=”Microsoft Bob” ItemProp