Download Internationalization - Java Programming Language - Lecture Slides and more Slides Computer Science in PDF only on Docsity!
Chapter 12: Internationalization
• Processing Date and Time
– Locale
– Date
– TimeZone
– Calendar and GregorianCalendar
– DateFormat and
SimpleDateFormat
• Formatting Numbers
• Resource Bundles
Java’s International Support
- Use Unicode (16-bit char)
UTF-8 (8-bit for ASCII Char and 24-bit for other char)
- Provide the Locale class to encapsulate information about a specific locale (such as date, time, and number).
- Use the ResourceBundle class to separate locale-specific information such as status messages and the GUI component labels from the program.
Creating a Locale
To create a Locale object, you can use the following constructor in Locale class:
Locale(String language, String country)
Locale(String language, String country, String variant)
Language: iso639 Country code: iso
Example:
new Locale(“en”, “US”); or new Locale(“fr”, “CA”);
See Locale class for more methods: getDefault(), …
The Date Class
- The Date class represents a specific instant in time, with millisecond precision.
- You can construct a Date object using one of
the following two constructors in this class:
public Date();
public Date(long time);
Creating a TimeZone
- You can also get a TimeZone object by using the
class method getTimeZone(), along with a time
zone ID.
- For example, the time zone ID for central
standard time is CST. Therefore, you can get a
CST TimeZone object with the following:
TimeZone tz = TimeZone.getTimeZone("CST");
The Calendar Class
- A Date object represents a specific instant in time with millisecond precision.
- Calendar is an abstract base class for converting between a Date object and a set of integer fields, such as year, month, day , hour,
minute, and second.
public final void set(int field, int value) public final void set(int year,int month, int date)
The get() Method
public final int get(int field)
This retrieves the value for a given time field.
The parameter field is a constant, such as
YEAR , MONTH , DAY , HOUR , MINUTE, SECOND ,
DAY_OF_WEEK , DAY_OF_MONTH ,
DAY_OF_YEAR , WEEK_OF_MONTH ,
WEEK_OF_YEAR , and so on.
GregorianCalendar Example
GregorianCalendar rightNow = new GregorianCalendar(); System.out.println("week of the year is "+rightNow.get(GregorianCalendar.WEEK_OF_YEAR));
This displays the week of the year for the current time.
DateFormat Formats
SHORT is completely numeric, such as
12.13.52 (for date) or 3:30pm (for time)
MEDIUM is longer, such as Jan 12, 1952
LONG is even longer, such as January 12, 1952
or 3:30:32pm
FULL is completely specified, such as
Tuesday, April 12, 1952 AD
or 3:30:42pm PST
Creating a DateFormat
You can use the getDateTimeInstance() method
to obtain a DateFormat object:
public static final DateFormat getDateTimeInstance (int dateStyle, int timeStyle, Locale aLocale)
This gets the date and time formatter with the given
formatting styles for the given locale.
Example 12.
Displaying a Clock
- Objective: Display current time based on the specified locale and time zone. The program can run as applet or application. The language, country, and time zone are passed to the program as command-line arguments like this:
javaw CurrentTimeApplet en US CST
CurrentTimeApplet
Run as Application Run as Applet
Example 12.
Displaying a Calendar
- Objective: Display the calendar based on
the specified locale. The user can
specify a locale from a combo box that
consists of a list of all the available
locales supported by the system.
CalendarApplet
Run as Application Run as Applet
The NumberFormat Class
- Use one of the factory class methods to
get a formatter.
1. Use getInstance() or getNumberInstance() to
get the normal number format.
2. Use getCurrencyInstance() to get the
currency number format.
3. Use getPercentInstance() to get a format
for displaying percentages. With this
format, a fraction like 0.53 is displayed
as 53%.
The NumberFormat Class
(cont.)
For example, to display a number in
percentages, you can use the following code
to create a formatter for the given locale.
NumberFormat percForm =
NumberFormat.getPercentInstance(locale);
You can then use percForm to format a
number into a string like this:
String s = percForm.format(0.075);