net.sourceforge.stripes.format
Class DateFormatter

java.lang.Object
  extended by net.sourceforge.stripes.format.DateFormatter
All Implemented Interfaces:
Formatter<Date>

public class DateFormatter
extends Object
implements Formatter<Date>

Implements a basic formatter for Date objects. Accepts several known types and patterns, as well as arbitrary patterns. Under the covers uses DateFormat and SimpleDateFormat objects from the java.text package - it is advised that you become familiar with these classes before attempting to use custom patterns.

Format types affect the kind of information that is output. The supported format types are (values are not case sensitive):

Format strings affect the format of the selected output. One of the following known values may be supplied as the format string (named values are not case sensitive). If the value is not one of the following, it is passed to SimpleDateFormat as a pattern string.

Author:
Tim Fennell

Field Summary
protected static Map<String,Integer> namedPatterns
          Maintains a map of named formats that can be used instead of patterns.
 
Constructor Summary
DateFormatter()
           
 
Method Summary
 String format(Date input)
          Formats a Date as a String using the rules supplied when the formatter was built.
 DateFormat getDateFormat()
          Gets the date format that will format the date.
 String getFormatPattern()
          Gets the named format string or date pattern to use to format the date.
 String getFormatType()
          Gets the format type to be used to render dates as Strings.
 Locale getLocale()
          Gets the locale that output String should be in.
 void init()
          Constructs the DateFormat used for formatting, based on the values passed to the various setter methods on the class.
 void setDateFormat(DateFormat dateFormat)
          Sets the date format that will format the date.
 void setFormatPattern(String formatPattern)
          Sets the named format string or date pattern to use to format the date.
 void setFormatType(String formatType)
          Sets the format type to be used to render dates as Strings.
 void setLocale(Locale locale)
          Sets the locale that output String should be in.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

namedPatterns

protected static final Map<String,Integer> namedPatterns
Maintains a map of named formats that can be used instead of patterns.

Constructor Detail

DateFormatter

public DateFormatter()
Method Detail

setFormatType

public void setFormatType(String formatType)
Sets the format type to be used to render dates as Strings.

Specified by:
setFormatType in interface Formatter<Date>

getFormatType

public String getFormatType()
Gets the format type to be used to render dates as Strings.


setFormatPattern

public void setFormatPattern(String formatPattern)
Sets the named format string or date pattern to use to format the date.

Specified by:
setFormatPattern in interface Formatter<Date>

getFormatPattern

public String getFormatPattern()
Gets the named format string or date pattern to use to format the date.


setLocale

public void setLocale(Locale locale)
Sets the locale that output String should be in.

Specified by:
setLocale in interface Formatter<Date>

getLocale

public Locale getLocale()
Gets the locale that output String should be in.


init

public void init()
Constructs the DateFormat used for formatting, based on the values passed to the various setter methods on the class. If the formatString is one of the named formats then a DateFormat instance is created of the specified type and format, otherwise a SimpleDateFormat is constructed using the pattern provided and the formatType is ignored.

Specified by:
init in interface Formatter<Date>
Throws:
StripesRuntimeException - if the formatType is not one of 'date', 'time' or 'datetime'.

getDateFormat

public DateFormat getDateFormat()
Gets the date format that will format the date. Subclasses that wish to alter the date format should override init(), call super.init(), and then obtain the date format object.


setDateFormat

public void setDateFormat(DateFormat dateFormat)
Sets the date format that will format the date. Subclasses that wish to set the date format should override init() and then set the date format object.


format

public String format(Date input)
Formats a Date as a String using the rules supplied when the formatter was built.

Specified by:
format in interface Formatter<Date>
Parameters:
input - an object of a type that the formatter knows how to format
Returns:
a non-null, String version of the input, formatted for the chosen locale


? Copyright 2005-2006, Stripes Development Team.