Date and time formatters convert internal time data into text strings for meaningful display. The Taligent date and time formatters:
- Handle timezones and daylight savings adjustments
- Support multiple calendars
- Support European date fields such as the number of the week in the year
- Like the number formatters:
- Support definition of formats with string patterns and can retrieve the normalized pattern
- Can parse anything they can format
Localized Formatting and Parsing Editing Format Patterns
Date Formatting Utilities
Localized Formatting and Parsing
The date and time formatters let you format the time according to the language, format, and time zone for a given country. The formatters also provide language-specific parsing.
The applet displays the date in two formats--the Java 1.0 representation ("1.0 Date") and the localized format generated by the Taligent date and time formatters ("1.1 Date").
To See This...
Do This...
Dates localized for the language and time zones of the selected locale.
- Pull down the Locale menu
- Try several different locales with the up and down arrow keys (on Windows) or the mouse button (on Macintosh)
The formatter can parse changes to the formatted Java 1.1 date and reflect it in the Java 1.0 date and the value in milliseconds.
- Select an English locale
- Replace the month name in the 1.1 Date field with "June"
The formatter formats correctly for different time zones within a country, for example, Pacific or Eastern time. It also switches automatically between standard time or daylight savings time based on the current date within the year.
- Select the U.S. locale
- Select the Time Format.
- Pull down the City menu and select different cities to see the time in different time zones
Note: The dates shown here are all based on the Gregorian calendar. The time formatting classes define an abstract calendar class that can be extended to support non-Gregorian calendars. This can be used by programmers, without special knowledge of which calendar is right for which country.
Editing Format Patterns
As with the number formatters, you can easily create and modify date and time formats by specifying string patterns. You can also add new fields to the format, and create formats used for business purposes in some countries.
To See This...
Do This...
The format pattern supports a variety of fields, and supports both full and abbreviated formats for some fields. The month, for example, can be numeric, abbreviated, or full.
- Select the Date Format and the English (U.S.) locale
- In the Pattern field, delete an "M"--the month changes to the abbreviated format
- Delete another "M"--the month changes to the numeric format
You can specify formats used in business documents, such as day of the year. You can also add unformatted strings to the pattern by enclosing them in single quotes.
- Select the month and day fields in the pattern ("MM dd")
- Replace it with the string (including the single quotes) "'day' D"
Date Formatting Utilities
The formatters also provide a number of utilities that you can use to build date and time widgets, including adding to or rolling fields.
To See This...
Do This...
"Rolling" a field prevents other fields from being affected by the change (where possible). In particular, when the maximum value is reached, the next value returns to the minimum. This works like resetting your digital clock.
- Select the Date Format and the English (U.S.) locale
- Select Month in the Date Fields menu
- Click on the Roll button
- Click the up-arrow button until the formatted date reaches December
- Click again--the month rolls to January but the year does not increment
"Adding" to a field causes other fields to be affected by the change as appropriate.
- Click on the Add button
- Click the up-arrow button until the formatted date reaches December
- Click again--the month rolls to January and the year does increment
Note that the formatter can handle a wide range of dates. (Note: Currently there is a bug that causes problems with dates before 1200 BC and after 5000 AD).
To See This...
Do This...
The formatted date changes to reflect changes in the millisecond value of the date.
- Select the Date Format and the English (U.S.) locale
- Type a "G" at the end of the pattern string to display the era
- Select the value in the Millis field and replace it with a "1"
- Add zeroes until you reach the year 5000 AD (you could also start at "-1" and add zeroes to go backwards in time)
The formatter supports many different fields you can use to create patterns. ASCII letters are reserved as pattern letters (unless enclosed in quotes), defined as the following:
Symbol
Meaning
Presentation
Example
G
era designator
(Text)
AD
y
year
(Number)
1996
M
month in year
(Text / Number)
July / 07
d
day in month
(Number)
10
k
hour in day
(Number)
24
H
hour in day, 0-based
(Number)
0
m
minute in hour
(Number)
30
s
second in minute
(Number)
55
S
millisecond
(Number)
978
E
day in week
(Text)
Tuesday
D
day in year
(Number)
189
F
day of week in month
(Number)
2 (2nd Wed in July)
w
week in year
(Number)
27
W
week in month
(Number)
2
a
am/pm marker
(Text)
PM
h
hour in am/pm
(Number)
12
K
hour in am/pm, 0-based
(Number)
0
z
time zone
(Text)
PST
'
escape for text
''
single quote
'
You can type in other dates in either date field or enter a completely new pattern to see different formatting behaviors. Try it out!
The source.
This page incorporates material or code copyrighted by Taligent, Inc. For more information on international resources, see their International Fact Sheet.