23
Chapter 12: Internationalization Processing Date and Time Processing Date and Time Locale Locale Date Date TimeZone TimeZone Calendar and GregorianCalendar Calendar and GregorianCalendar DateFormat and DateFormat and SimpleDateFormat SimpleDateFormat Formatting Numbers Formatting Numbers Resource Bundles Resource Bundles

Chapter 12: Internationalization Processing Date and Time Processing Date and Time Locale Date TimeZone Calendar and GregorianCalendar DateFormat

Embed Size (px)

Citation preview

Page 1: Chapter 12: Internationalization Processing Date and Time Processing Date and Time  Locale  Date  TimeZone  Calendar and GregorianCalendar  DateFormat

Chapter 12: Internationalization

Processing Date and Time Processing Date and Time LocaleLocaleDateDateTimeZoneTimeZoneCalendar and GregorianCalendarCalendar and GregorianCalendarDateFormat and SimpleDateFormatDateFormat and SimpleDateFormat

Formatting NumbersFormatting Numbers Resource BundlesResource Bundles

Page 2: Chapter 12: Internationalization Processing Date and Time Processing Date and Time  Locale  Date  TimeZone  Calendar and GregorianCalendar  DateFormat

Java’s International Support

1.1. Use Use Unicode (16-bit char)Unicode (16-bit char)

UTF-8 (8-bit for ASCII Char and 24-bit for UTF-8 (8-bit for ASCII Char and 24-bit for other char)other char)

2. Provide the Locale class to encapsulate 2. Provide the Locale class to encapsulate information about a specific locale (such as information about a specific locale (such as date, time, and number). date, time, and number).

3. Use the ResourceBundle class to separate 3. Use the ResourceBundle class to separate locale-specific information such as status locale-specific information such as status messages and the GUI component labels from messages and the GUI component labels from the program. the program.

Page 3: Chapter 12: Internationalization Processing Date and Time Processing Date and Time  Locale  Date  TimeZone  Calendar and GregorianCalendar  DateFormat

The Locale Class A Locale object represents a specific A Locale object represents a specific geographical, political, or cultural region. geographical, political, or cultural region. An operation that requires a Locale to An operation that requires a Locale to perform its task is called perform its task is called locale-sensitive.locale-sensitive. You can use Locale to tailor You can use Locale to tailor information to the user. information to the user.

Page 4: Chapter 12: Internationalization Processing Date and Time Processing Date and Time  Locale  Date  TimeZone  Calendar and GregorianCalendar  DateFormat

Creating a Locale

To create a Locale object, you can use theTo create a Locale object, you can use thefollowing constructor in Locale class: following constructor in Locale class:

Locale(String language, String country)Locale(String language, String country)

Locale(String language, String country, String variant)Locale(String language, String country, String variant)

Language: iso639 Country code: iso3166Language: iso639 Country code: iso3166

Example:Example:

new Locale(“en”, “US”); or new Locale(“fr”, “CA”); new Locale(“en”, “US”); or new Locale(“fr”, “CA”);

See Locale class for more methods: getDefault(), …See Locale class for more methods: getDefault(), …

Page 5: Chapter 12: Internationalization Processing Date and Time Processing Date and Time  Locale  Date  TimeZone  Calendar and GregorianCalendar  DateFormat

The Date Class The The DateDate class represents a specific instant in class represents a specific instant in time, with millisecond precision. time, with millisecond precision. You can construct a You can construct a DateDate object using one of object using one of the following two constructors in this class:the following two constructors in this class:

public Date();

public Date(long time);

Page 6: Chapter 12: Internationalization Processing Date and Time Processing Date and Time  Locale  Date  TimeZone  Calendar and GregorianCalendar  DateFormat

The TimeZone Class The class The class TimeZoneTimeZone represents a time zone represents a time zone offset; it also figures out daylight-saving time.offset; it also figures out daylight-saving time. Typically, you can get a Typically, you can get a TimeZoneTimeZone object using object using its class method its class method getDefault()getDefault(), which creates a , which creates a TimeZoneTimeZone object based on the time zone in which object based on the time zone in which the program is running.the program is running.

Page 7: Chapter 12: Internationalization Processing Date and Time Processing Date and Time  Locale  Date  TimeZone  Calendar and GregorianCalendar  DateFormat

Creating a TimeZone You can also get a You can also get a TimeZoneTimeZone object by using the object by using the class method class method getTimeZone()getTimeZone(), along with a time , along with a time zone ID. zone ID. For example, the time zone ID for central For example, the time zone ID for central standard time is standard time is CSTCST. Therefore, you can get a . Therefore, you can get a CST CST TimeZoneTimeZone object with the following: object with the following:

TimeZone tz = TimeZone.getTimeZone("CST");TimeZone tz = TimeZone.getTimeZone("CST");

Page 8: Chapter 12: Internationalization Processing Date and Time Processing Date and Time  Locale  Date  TimeZone  Calendar and GregorianCalendar  DateFormat

The Calendar Class A A DateDate object represents a specific instant in time with millisecond object represents a specific instant in time with millisecond precision. precision. CalendarCalendar is an abstract base class for converting between a is an abstract base class for converting between a DateDate object object and a set of integer fields, such as and a set of integer fields, such as yearyear, , monthmonth, , dayday, , hourhour, , minuteminute, and , and secondsecond.. For example:For example:

public final void set(int field, int value)public final void set(int field, int value)public final void set(int year,int month, int date)public final void set(int year,int month, int date)

Page 9: Chapter 12: Internationalization Processing Date and Time Processing Date and Time  Locale  Date  TimeZone  Calendar and GregorianCalendar  DateFormat

The GregorianCalendar Class Subclasses of Subclasses of CalendarCalendar interpret a interpret a DateDate according to according to the rules of a specific calendar system. For example:the rules of a specific calendar system. For example:

GregorianCalendar(TimeZone tz, Locale locale)GregorianCalendar(TimeZone tz, Locale locale)

This constructs a This constructs a GregorianCalendarGregorianCalendar object based object basedon the current time in the given time zone with theon the current time in the given time zone with thegiven locale.given locale.

Page 10: Chapter 12: Internationalization Processing Date and Time Processing Date and Time  Locale  Date  TimeZone  Calendar and GregorianCalendar  DateFormat

The get() Method

public final int get(int field)public final int get(int field)

This retrieves the value for a given time field. This retrieves the value for a given time field. The parameter field is a constant, such as The parameter field is a constant, such as YEARYEAR, , MONTHMONTH, , DAYDAY, , HOURHOUR, , MINUTEMINUTE, , SECONDSECOND, , DAY_OF_WEEKDAY_OF_WEEK, , DAY_OF_MONTHDAY_OF_MONTH, , DAY_OF_YEARDAY_OF_YEAR, , WEEK_OF_MONTHWEEK_OF_MONTH, , WEEK_OF_YEARWEEK_OF_YEAR, and so on., and so on.

Page 11: Chapter 12: Internationalization Processing Date and Time Processing Date and Time  Locale  Date  TimeZone  Calendar and GregorianCalendar  DateFormat

GregorianCalendar Example

GregorianCalendar rightNow = new GregorianCalendar();GregorianCalendar rightNow = new GregorianCalendar();System.out.println("week of the year is System.out.println("week of the year is "+rightNow.get(GregorianCalendar.WEEK_OF_YEAR));"+rightNow.get(GregorianCalendar.WEEK_OF_YEAR));

This displays the week of the year for the current This displays the week of the year for the current time.time.

Page 12: Chapter 12: Internationalization Processing Date and Time Processing Date and Time  Locale  Date  TimeZone  Calendar and GregorianCalendar  DateFormat

The DateFormat Class

The The DateFormatDateFormat class is an abstract class that class is an abstract class that provides many class methods for obtaining provides many class methods for obtaining default date and time formatters based on the default date and time formatters based on the default or a given locale and a number of default or a given locale and a number of formatting styles, including formatting styles, including FULLFULL, , LONGLONG, , MEDIUMMEDIUM, and , and SHORTSHORT..

Page 13: Chapter 12: Internationalization Processing Date and Time Processing Date and Time  Locale  Date  TimeZone  Calendar and GregorianCalendar  DateFormat

DateFormat Formats SHORTSHORT is completely numeric, such as is completely numeric, such as

12.13.5212.13.52 (for date) or (for date) or 3:30pm (for time)3:30pm (for time)

MEDIUMMEDIUM is longer, such as is longer, such as Jan 12, 1952 Jan 12, 1952

LONGLONG is even longer, such as is even longer, such as January 12, January 12, 19521952 or or 3:30:32pm 3:30:32pm

FULLFULL is completely specified, such as is completely specified, such asTuesday, April 12, 1952 ADTuesday, April 12, 1952 ADor or 3:30:42pm PST3:30:42pm PST

Page 14: Chapter 12: Internationalization Processing Date and Time Processing Date and Time  Locale  Date  TimeZone  Calendar and GregorianCalendar  DateFormat

Creating a DateFormat

You can use the You can use the getDateTimeInstance()getDateTimeInstance() method methodto obtain a to obtain a DateFormatDateFormat object: object:

public static final DateFormat getDateTimeInstancepublic static final DateFormat getDateTimeInstance (int dateStyle, int timeStyle, Locale aLocale) (int dateStyle, int timeStyle, Locale aLocale)

This gets the date and time formatter with the givenThis gets the date and time formatter with the givenformatting styles for the given locale. formatting styles for the given locale.

Page 15: Chapter 12: Internationalization Processing Date and Time Processing Date and Time  Locale  Date  TimeZone  Calendar and GregorianCalendar  DateFormat

The SimpleDateFormat ClassThe date and time formatting subclass, such as The date and time formatting subclass, such as SimpleDateFormatSimpleDateFormat, enables you to choose any user-, enables you to choose any user-defined patterns for date and time formatting. To specify defined patterns for date and time formatting. To specify the time format, use a time pattern string:the time format, use a time pattern string:

formatter = new SimpleDateFormat("yyyy.MM.dd Gformatter = new SimpleDateFormat("yyyy.MM.dd G 'at' hh:mm:ss Z"); 'at' hh:mm:ss Z");

1997.11.12 AD at 04:10:18 PST1997.11.12 AD at 04:10:18 PST

Page 16: Chapter 12: Internationalization Processing Date and Time Processing Date and Time  Locale  Date  TimeZone  Calendar and GregorianCalendar  DateFormat

Example 12.1Displaying a Clock

Objective: Display current time based on the Objective: Display current time based on the specified locale and time zone. The program specified locale and time zone. The program can run as applet or application. The can run as applet or application. The language, country, and time zone are passed language, country, and time zone are passed to the program as command-line arguments to the program as command-line arguments like this:like this:

javaw CurrentTimeApplet en US CSTjavaw CurrentTimeApplet en US CST

CurrentTimeAppletCurrentTimeApplet

Run as AppletRun as AppletRun as ApplicationRun as Application

Page 17: Chapter 12: Internationalization Processing Date and Time Processing Date and Time  Locale  Date  TimeZone  Calendar and GregorianCalendar  DateFormat

Example 12.2Displaying a Calendar

Objective: DObjective: Display the calendar based on isplay the calendar based on the specified locale. The user can the specified locale. The user can specify a locale from a combo box that specify a locale from a combo box that consists of a list of all the available consists of a list of all the available locales supported by the system. locales supported by the system.

CalendarAppletCalendarApplet

Run as AppletRun as AppletRun as ApplicationRun as Application

Page 18: Chapter 12: Internationalization Processing Date and Time Processing Date and Time  Locale  Date  TimeZone  Calendar and GregorianCalendar  DateFormat

Formatting Numbers

Formatting numbers as currency or Formatting numbers as currency or percentages is highly locale dependent. percentages is highly locale dependent.

For example, number 5000.50 is displayed For example, number 5000.50 is displayed as $5,000.50 in the US currency, but the as $5,000.50 in the US currency, but the same number is displayed as 5 000,50 F same number is displayed as 5 000,50 F in the French currency.in the French currency.

Page 19: Chapter 12: Internationalization Processing Date and Time Processing Date and Time  Locale  Date  TimeZone  Calendar and GregorianCalendar  DateFormat

The NumberFormat Class Use one of the factory class methods Use one of the factory class methods

to get a formatter. to get a formatter. 1.1. Use Use getInstance()getInstance() or or getNumberInstance()getNumberInstance() to to

get the normal number format. get the normal number format. 2.2. Use Use getCurrencyInstance()getCurrencyInstance() to get the to get the

currency number format. currency number format. 3.3. Use Use getPercentInstance()getPercentInstance() to get a format to get a format

for displaying percentages. With this for displaying percentages. With this format, a fraction like 0.53 is displayed format, a fraction like 0.53 is displayed as 53%.as 53%.

Page 20: Chapter 12: Internationalization Processing Date and Time Processing Date and Time  Locale  Date  TimeZone  Calendar and GregorianCalendar  DateFormat

The NumberFormat Class (cont.)

For example, to display a number in For example, to display a number in percentages, you can use the following code percentages, you can use the following code to create a formatter for the given locale.to create a formatter for the given locale.

NumberFormat percForm = NumberFormat percForm = NumberFormat.getPercentInstance(locale);NumberFormat.getPercentInstance(locale);

You can then use You can then use percFormpercForm to format a to format a number into a string like this:number into a string like this:

String s = percForm.format(0.075);String s = percForm.format(0.075);

Page 21: Chapter 12: Internationalization Processing Date and Time Processing Date and Time  Locale  Date  TimeZone  Calendar and GregorianCalendar  DateFormat

Example 12.3Formatting Numbers

Objective: This example creates a Objective: This example creates a mortgage calculator similar to the one in mortgage calculator similar to the one in Example 10.1, "Using Applets." This Example 10.1, "Using Applets." This new mortgage calculator allows the user new mortgage calculator allows the user to choose locales, and displays to choose locales, and displays numbers in locale-sensitive format. numbers in locale-sensitive format.

NumberFormattingDemoNumberFormattingDemo

Run as AppletRun as AppletRun as ApplicationRun as Application

Page 22: Chapter 12: Internationalization Processing Date and Time Processing Date and Time  Locale  Date  TimeZone  Calendar and GregorianCalendar  DateFormat

Resource Bundles (Optional)

A A resource bundleresource bundle is a Java class file or a text is a Java class file or a text file that provides locale-specific information. This file that provides locale-specific information. This information can be accessed by Java programs information can be accessed by Java programs dynamically. dynamically.

When your program needs a locale-specific When your program needs a locale-specific resource, a message string for example, your resource, a message string for example, your program can load the string from the resource program can load the string from the resource bundle that is appropriate for the desired locale. bundle that is appropriate for the desired locale. In this way, you can write program code that is In this way, you can write program code that is largely independent of the user's locale isolating largely independent of the user's locale isolating most, if not all, of the locale-specific information most, if not all, of the locale-specific information in resource bundles.in resource bundles.

Page 23: Chapter 12: Internationalization Processing Date and Time Processing Date and Time  Locale  Date  TimeZone  Calendar and GregorianCalendar  DateFormat

Example 12.4Using Resource Bundles

Objective: This example modifies the Objective: This example modifies the NumberFormattingDemo program in NumberFormattingDemo program in Example 12.3 to display messages, title, Example 12.3 to display messages, title, and button labels in English and French and button labels in English and French languages.languages.

ResourceBundleDemoResourceBundleDemo

Run as AppletRun as Applet

Run as ApplicationRun as Application

Res.propertiesRes.properties

Res_fr.propertiesRes_fr.properties