SLIDE 90 San Jose, CA, August-September 1999 15th International Unicode Conference 90 Developing Global Applications in Java
SimpleDateFormat
Only concrete subclass of DateFormat Output controlled by a pattern string
- Groups of letters mark positions of elements:
G=era (e.g., BC or AD), y=year, M=month, d=day, E=day of week, h=hour (12-hour clock), H=hour (24-hour clock), m=minute, s=second, a=AM/PM, z=time zone, etc.
- Literal characters enclosed in single quotes
- Number of letters in group controls size
For a numeric value, # of letters is minimum # of digits For a textual value, 4 letters means spell it out in words, less than 4 uses abbreviation For a value that be rendered either way, 1 or 2 letters means digits and 3 or more letters means text
“h:mm:ss a zzzz, EEEE, MMMM d, yyyy G” produces “9:04:36 AM Pacific Daylight Time, Sunday, August 1, 1999 AD”
Again, the implementation class of DateFormat, SimpleDateFormat, is also public, allowing finer-grained control over date/time formatting than the DateFormat factory methods give you. SimpleDateFormat’s behavior is controlled by a pattern string that acts as a template for the desired result. The pattern string includes tokens specifying different possible “fields” of the date, such as day or month or hour of day (there are many to choose from), punctuation or boilerplate text, and their relative orders. The tokens also usually allow for several alternative representations for their value. The canned date formats are all based on pattern strings in resources.