3.3 Dates, Numbers & Currencies
In this lesson we use classes from the
java.text package to correctly format or parse dates, numbers, and currency values for a specific locale including usage of the appropriate methods to use the default locale or a specific locale.
Lets take a look at the points outlined at the Oracle Website for this part of the certification.
- Section 3: API Contents
- Use standard J2SE APIs in the java.text package to correctly format or parse dates, numbers, and currency values for a specific locale; and, given a scenario, determine the appropriate methods to use if you want to use the default locale or a specific locale. Describe the purpose and use of the java.util.Locale class.
The table below gives a brief description of each class that we will be using in this lesson:
java.util.Date Class represents a specific instant in time that gives us millisecond precision. The
Date class is intended to reflect an internationally standardized Computer Date and
Time which starts from
1 January 1970 00:00:00 UTC (Universal Time Coordinated).
Date object creation using the long argument is meant to reflect dates in milliseconds strarting from this
date. Looking at the official documentation for the
java.util.Date Class we can see there is one other non-deprecated
Date constructor that creates a
Date object that represents a
date and time when the the object was allocated to the nearest millisecond.
A lot of the methods in the
Date class were deprecated from the language in the
JDK 1.1 release as the
Date class didn't do a very good job of handling internationalisation
and localisation. The
java.util.Calendar was introduced into the language in the
JDK 1.1 release to replace the
Date class for easier date manipulation,
internationalisation and localisation and we will look at this class in the next part of this lesson.
Date object still serves some purpose though:
- It's an easy way to get the current date and time if you don't want to do a lot of manipulation, localise or internationalise the date and time.
- Good for getting a universal time not affected by time zones.
- Can be useful for simple date comparisons.
DateFormatclass needs a
Dateobject for some of its formatting methods. So a
Dateobject can act as a bridge between date manipulation in the
Calendarclass and formatting of the manipulated date in the
See Date Example for code examples.
We can manipulate
Date objects using the
setTime() methods of the
java.util.Date class, but this is a cumbersome way to manipualte dates. The
java.util.Calendar class is an easier way to manipulate dates and offers a lot of methods to do it.To get instances of the
java.util.Calendar class we have to use one of the overloaded
getInstance static factory methods.
See Calendar Example for code examples.
java.util.Locale class allows us to create an object that represents a specific geographical, political, or cultural region of the world. We can then use the
Locale object in
conjunction with the
java.util.NumberFormat classes to get locale specific dates, times, numbers and currencies for that locale.
Operations that require specific locale information to perform tasks are known as locale-sensitive and as such use a
Locale object to tailor user information accordingly. As
examples, this could include the way dates are displayed, the way numbers are formatted or the way currencies are viewed.
So once we set a specific locale we can use this object in conjunction with the
java.util.NumberFormat classes to format locale-sensitive
data for our users. In this way we can handle internationalisation and localisation when we want it, or do nothing and automatically use a default locale otherwise.
See Locale Example for code examples.
java.util.DateFormat class allows us to, not suprisingly to get formatted dates and times as well as parsing dates and times in a language-independent manner. On inspection of the official
documentation for the
DateFormat class we can see it is abstract and so cannot be instantiated directly. To get instances of the
DateFormat class we have to use one of the
overloaded static factory methods available and we can also pass options to the constructor to return dates and times with different levels of detail.
The table below just shows the formats we can pass to the static factory methods of the
|Date Format Style||Description|
|Completely numeric representation for a date or time.|
|A longer representation than |
|A longer representation than |
|Longest representation for a date or time.|
See Date Format Example for code examples.
java.util.NumberFormat class allows us to get formatted number as well as parsing numbers for any locale. On inspection of the official documentation for the
class we can see it is abstract and so cannot be instantiated directly. To get instances of the
NumberFormat class we have to use one of the overloaded static factory methods
available and we can also pass options to the constructor to return formatted numbers or currencies.
See Number Format Example for code examples.
See Locale-sensitive Dates & Numbers for code examples of locale sensive data using a combination of the classes discussed in this lesson.