public abstract StringBufferformat(Object obj, StringBuffer
appendTo, FieldPosition pos)
Adds the formatted string for obj to the end of appendTo. The object can
be either a Date or a Number whose longValue is a time in milliseconds.
The pos argument is a FieldPosition object that tracks the starting and ending index for a specific field
within the formatted output. You create a FieldPosition object by passing an integer code that represents
the field that the object should track. These codes are static fields in DateFormat, such as MINUTE_FIELD
or MONTH_FIELD. Suppose you construct a FieldPosition object pos with MINUTE_FIELD and then
pass it as an argument to a format method. When format returns, the getBeginIndex and
getEndIndex methods of pos will return the start and end indices of the characters representing minutes
within the formatted string. A specific formatter could also use the FieldPosition object to align the
represented field within the formatted string. To make that happen, you would first invoke the
setBeginIndex and setEndIndex methods of pos, passing the indices where you would like that field
to start and end in the formatted string. Exactly how the formatter aligns the formatted text depends on the
formatter implementation.
A DateFormat object can also be used to parse dates. Date parsing can be lenient or not, depending on your
preference. Lenient date parsing is as forgiving as it can be, whereas strict parsing requires the format and
information to be proper and complete. The default is to be lenient. You can use setLenient to set
leniency to be true or false. You can test leniency via isLenient.
The parsing methods are
public Dateparse(String text)throws ParseException
Tries to parse text into a date and/or time. If successful, a Date object is
returned; otherwise, a ParseException is thrown.
public abstract Dateparse(String text, ParsePosition pos)
Tries to parse text into a date and/or time. If successful, a Date object is
returned; otherwise, returns a null reference. When the method is called,
pos is the position at which to start parsing; at the end it will either be
positioned after the parsed text or will remain unchanged if an error occurred.
public ObjectparseObject(String text, ParsePosition pos)
Returns the result of parse(text,pos). This method is provided to
fulfill the generic contract of Format.
The class java.text.SimpleDateFormat is a concrete implementation of DateFormat that is used
in many locales. If you are writing a DateFormat class, you may find it useful to extend
SimpleDateFormat. SimpleDateFormat uses methods in the DateFormatSymbols class to get
localized strings and symbols for date representation. When formatting or parsing dates, you should usually
not create SimpleDateFormat objects; instead, you should use one of the "get instance" methods to return
an appropriate formatter.
DateFormat has protected fields calendar and numberFormat that give direct access to the values
publicly manipulated with the set and get methods.
Exercise 24.3: Write a program that takes a string argument that is parsed into the date to print, and print that
date in all possible styles. How lenient will the date parsing be?