com.jgoodies.validation.tutorial.formatted.format
Class RelativeDateFormat

java.lang.Object
  extended by java.text.Format
      extended by java.text.DateFormat
          extended by com.jgoodies.validation.tutorial.formatted.format.RelativeDateFormat
All Implemented Interfaces:
Serializable, Cloneable

public final class RelativeDateFormat
extends DateFormat

A Format that provides special handling for output shortcuts and relative input.

If output shortcuts are enabled, Yesterday, Today and Tomorrow are formatted using their localized human-language print strings.

If relative input is allowed, the parser accepts signed integers that encode a date relative to today; this input would otherwise be considered invalid.

This class is intended for demonstration purposes only. It is superceded by the RelativeDateFormatter.

Version:
$Revision: 1.12 $
Author:
Karsten Lentzsch
See Also:
ValidationUtils, Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.text.DateFormat
DateFormat.Field
 
Field Summary
static String KEY_TODAY
          The resource key used to lookup the localized text for "Today".
static String KEY_TOMORROW
          The resource key used to lookup the localized text for "Tomorrow".
static String KEY_YESTERDAY
          The resource key used to lookup the localized text for "Yesterday".
 
Fields inherited from class java.text.DateFormat
AM_PM_FIELD, calendar, DATE_FIELD, DAY_OF_WEEK_FIELD, DAY_OF_WEEK_IN_MONTH_FIELD, DAY_OF_YEAR_FIELD, DEFAULT, ERA_FIELD, FULL, HOUR_OF_DAY0_FIELD, HOUR_OF_DAY1_FIELD, HOUR0_FIELD, HOUR1_FIELD, LONG, MEDIUM, MILLISECOND_FIELD, MINUTE_FIELD, MONTH_FIELD, numberFormat, SECOND_FIELD, SHORT, TIMEZONE_FIELD, WEEK_OF_MONTH_FIELD, WEEK_OF_YEAR_FIELD, YEAR_FIELD
 
Constructor Summary
RelativeDateFormat()
          Constructs a wrapped format that provides special handling for output shortcuts and relative input.
RelativeDateFormat(DateFormat delegate, boolean useOutputShortcuts, boolean allowRelativeInput)
          Constructs a wrapped format that provides special handling for output shortcuts and relative input.
 
Method Summary
 StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition)
          Formats a Date into a date/time string.
static Format getDefaultDisplayDateFormat()
          Creates and answers the default Format that is used to edit instances of Date in the UI: It converts null to empty Strings, uses the short DateFormat, uses shortcuts for output and does not accepts relative input.
static Format getDefaultEditDateFormat()
          Creates and answers the default Format that is used to edit instances of Date in the UI: It converts null to empty Strings, uses the short DateFormat, uses shortcuts for output and accepts relative input.
static Format getDefaultPrintDateFormat()
          Creates and answers the default Format that is used to edit instances of Date in the UI: It converts null to empty Strings, uses the short DateFormat, does not uses shortcuts for output and does not accepts relative input.
 ResourceBundle getResourceBundle()
           
 Date parse(String source, ParsePosition pos)
          Parse a date/time string according to the given parse position.
 void setResourceBundle(ResourceBundle newBundle)
           
 
Methods inherited from class java.text.DateFormat
clone, equals, format, format, getAvailableLocales, getCalendar, getDateInstance, getDateInstance, getDateInstance, getDateTimeInstance, getDateTimeInstance, getDateTimeInstance, getInstance, getNumberFormat, getTimeInstance, getTimeInstance, getTimeInstance, getTimeZone, hashCode, isLenient, parse, parseObject, setCalendar, setLenient, setNumberFormat, setTimeZone
 
Methods inherited from class java.text.Format
format, formatToCharacterIterator, parseObject
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

KEY_YESTERDAY

public static final String KEY_YESTERDAY
The resource key used to lookup the localized text for "Yesterday".

See Also:
Constant Field Values

KEY_TODAY

public static final String KEY_TODAY
The resource key used to lookup the localized text for "Today".

See Also:
Constant Field Values

KEY_TOMORROW

public static final String KEY_TOMORROW
The resource key used to lookup the localized text for "Tomorrow".

See Also:
Constant Field Values
Constructor Detail

RelativeDateFormat

public RelativeDateFormat()
Constructs a wrapped format that provides special handling for output shortcuts and relative input.


RelativeDateFormat

public RelativeDateFormat(DateFormat delegate,
                          boolean useOutputShortcuts,
                          boolean allowRelativeInput)
Constructs a wrapped format that provides special handling for output shortcuts and relative input.

Parameters:
delegate - the DateFormat to handle format and parse dates
useOutputShortcuts - true indicates that dates are formatted with shortcuts for yesterday, today, and tomorrow, where false always converts using absolute numbers for day, month and year.
allowRelativeInput - true indicates that the parser accepts signed integers that encode a date relative to today; if false such input is considered invalid
Method Detail

getDefaultEditDateFormat

public static Format getDefaultEditDateFormat()
Creates and answers the default Format that is used to edit instances of Date in the UI: It converts null to empty Strings, uses the short DateFormat, uses shortcuts for output and accepts relative input.

Returns:
the default date format that converts Dates to Strings

getDefaultDisplayDateFormat

public static Format getDefaultDisplayDateFormat()
Creates and answers the default Format that is used to edit instances of Date in the UI: It converts null to empty Strings, uses the short DateFormat, uses shortcuts for output and does not accepts relative input.

Returns:
the default date format that converts Dates to Strings

getDefaultPrintDateFormat

public static Format getDefaultPrintDateFormat()
Creates and answers the default Format that is used to edit instances of Date in the UI: It converts null to empty Strings, uses the short DateFormat, does not uses shortcuts for output and does not accepts relative input.

Returns:
the default date format that converts Dates to Strings

format

public StringBuffer format(Date date,
                           StringBuffer toAppendTo,
                           FieldPosition fieldPosition)
Formats a Date into a date/time string. In addition to the delegate's behavior, this method formats the dates for yesterday, today and tomorrow to the natural language strings.

Specified by:
format in class DateFormat
Parameters:
date - a Date to be formatted into a date/time string.
toAppendTo - the string buffer for the returning date/time string.
fieldPosition - keeps track of the position of the field within the returned string. On input: an alignment field, if desired. On output: the offsets of the alignment field. For example, given a time text "1996.07.10 AD at 15:08:56 PDT", if the given fieldPosition is DateFormat.YEAR_FIELD, the begin index and end index of fieldPosition will be set to 0 and 4, respectively. Notice that if the same time field appears more than once in a pattern, the fieldPosition will be set for the first occurrence of that time field. For instance, formatting a Date to the time string "1 PM PDT (Pacific Daylight Time)" using the pattern "h a z (zzzz)" and the alignment field DateFormat.TIMEZONE_FIELD, the begin index and end index of fieldPosition will be set to 5 and 8, respectively, for the first occurrence of the timezone pattern character 'z'.
Returns:
the formatted date/time string.

parse

public Date parse(String source,
                  ParsePosition pos)
Parse a date/time string according to the given parse position. For example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date that is equivalent to Date(837039928046).

By default, parsing is lenient: If the input is not in the form used by this object's format method but can still be parsed as a date, then the parse succeeds. Clients may insist on strict adherence to the format by calling setLenient(false).

In addition to the delegate's behavior, this methods accepts signed integers interpreted as days relative to today.

Specified by:
parse in class DateFormat
Parameters:
source - The date/time string to be parsed
pos - On input, the position at which to start parsing; on output, the position at which parsing terminated, or the start position if the parse failed.
Returns:
A Date, or null if the input could not be parsed
See Also:
DateFormat.setLenient(boolean)

getResourceBundle

public ResourceBundle getResourceBundle()

setResourceBundle

public void setResourceBundle(ResourceBundle newBundle)


Copyright © 2003-2008 JGoodies Karsten Lentzsch. All Rights Reserved.