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 DateFormatter and call java.text.DateFormat.getDateInstance( ) to set the format to the formatting style of the current locale.

public DateFormatter(java.text.DateFormat format)

Create a new DateFormatter with the specified format.

Get Java Swing, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.