Java Reference
In-Depth Information
such boundaries in Unicode text, such as when you are implementing a word-
wrapping algorithm.
Example 7-3 shows a class that uses the NumberFormat and DateFormat classes to
display a hypothetical stock portfolio to the user following local conventions. The
program uses various NumberFormat and DateFormat object to format (using the
format() method) different types of numbers and dates. These Format objects all
operate using the default locale but could have been created with an explicitly
specified locale. The program displays information about a hypothetical stock
portfolio, formatting dates and numbers and monetary values according to the cur-
rent or the specified locale. The following listing shows the output of this program
in American, British, and French locales:
% java com.davidflanagan.examples.i18n.Portfolio en US
Portfolio value at July 9, 2000 9:08:48 PM PDT:
Symbol Shares Purchased
At
Quote
Change
XXX 400 Feb 3, 2000
$11.90 $13.00 9%
YYY
1,100
Mar 2, 2000
$71.09 $27.25 -62%
ZZZ
6,000
May 17, 2000
$23.37 $89.12 281%
% java com.davidflanagan.examples.i18n.Portfolio en GB
Portfolio value at 09 July 2000 21:08:55 PDT:
Symbol Shares Purchased
At
Quote
Change
XXX 400 03-Feb-00
£11.90 £13.00 9%
YYY 1,100
02-Mar-00 £71.09 £27.25 -62%
ZZZ 6,000
17-May-00 £23.37 £89.12 281%
% java com.davidflanagan.examples.i18n.Portfolio fr FR
Portfolio value at 9 juillet 2000 21:09:03 PDT:
Symbol Shares Purchased
At
Quote
Change
XXX
400
3 févr. 00
11,90 F 13,00 F
9%
YYY
1 100 2 mars 00
71,09 F 27,25 F
-62%
ZZZ
6 000 17 mai 00
23,37 F 89,12 F
281%
Example 7−3: Portfolio.java
package com.davidflanagan.examples.i18n;
import java.text.*;
import java.util.*;
import java.io.*;
/**
* A partial implementation of a hypothetical stock portfolio class.
* We use it only to demonstrate number and date internationalization.
**/
public class Portfolio {
EquityPosition[] positions;
Date lastQuoteTime = new Date();
public Portfolio(EquityPosition[] positions, Date lastQuoteTime) {
this.positions = positions;
this.lastQuoteTime = lastQuoteTime;
}
public void print(PrintWriter out) {
// Obtain NumberFormat and DateFormat objects to format our data.
NumberFormat number = NumberFormat.getInstance();
NumberFormat price = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
Search WWH ::




Custom Search