The DateFormatter Class
DateFormatter is a subclass of InternationalFormatter that uses an object of
type java.text.DateFormat as its
format. JFormattedTextField instantiates a DateFormatter for itself if you pass a
java.util.Date or a java.text.DateFormat into its constructor.
With a java.util.Date, the format is
localized to the default locale. This is usually what you want, but if
you want your program to use the same date format no matter where in the
world it is run, construct your JFormattedTextField with a specific java.text.DateFormat.
Most of the DateFormatter
implementation is concerned with providing support to increment and
decrement subfields of any date from the keyboard. It does a nice job
with this. If you haven’t done so yet, you might want run the SimpleFTF program and play with the date
field.
Properties
DateFormatter does not define
any properties beyond those it inherits (see Table 20-6). The minimum and maximum properties can be handy if you want
the field to be restricted to a specific range of dates.
Constructors
- public DateFormatter( )
Create a new
DateFormatterand calljava.text.DateFormat.getDateInstance( )to set theformatto the formatting style of the current locale.- public DateFormatter(java.text.DateFormat format)
Create a new
DateFormatterwith the specifiedformat.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access