shared.formatting
Class DateFormatter

java.lang.Object
  extended by shared.formatting.DateFormatter

public class DateFormatter
extends java.lang.Object

This class reformats dates into a standard form.

it reformats a string having the form MM/DD/YY or MM/DD/YYYY> to the form MM/DD/YYYY. It handles arbitrary non-digit input separators for parsing; a single "/" character is the default.

Conversion between two- and four-digit years is done by specifying a "first year in the epoch". Any two-digit year values are assumed to begin in the 100 years starting in that year. If no starting year for the epoch is provided, a default epoch beginning 49 years before the current year is assumed.

As an example, assume that the first year in the epoch is taken to be 1945. Any two digit years from "45" to "99" are assumed to be 1945 to 1999 (in the "first century"). All years from "00" to "44" are assumed to be 2000 to 2044 (in the "second century").

The date parsing itself uses Java's regular expression classes. The instance constructor uses the separator character provided by the caller to create regular expression patterns which will match the separator requested.

The 2- to 4-digit year processing is done by creating an instance of the inner Epoch class, initialized to perform conversions within the requested range of years.

The static variables are used to provide constant data shared among all class instances. The instance variables are set only in the constructors, and are not changed after that. This means that no synchronization is required when accessing either class or instance data from separate threads.


Nested Class Summary
protected  class DateFormatter.Epoch
          Convert between two- and four-digit years using a single year as the start of a two-digit epoch.
 
Field Summary
private static java.lang.String defaultSeparator
          Default string which separates input dates.
private  DateFormatter.Epoch epoch
          The DateFormatter.Epoch in which to interpret two-digit years
private static java.lang.String fourDigitYear
          Four-digit year pattern: exactly four digits.
private static java.lang.String monthDay
          Month and day pattern: one or two digits.
private  java.util.regex.Pattern monthDayYear
          Month-day-year pattern, compiled with given input separator.
private  java.util.regex.Pattern monthYear
          Month-year pattern, compiled with given input separator.
private static java.lang.String twoDigitYear
          Two-digit year pattern: exactly two digits.
private  java.util.regex.Pattern year
          Year pattern: two or four digits, no separator.
 
Constructor Summary
DateFormatter()
          Default constructor.
DateFormatter(int firstYearInEpoch, java.lang.String inputSeparator)
          Constructor which provides for alternative separator and epoch.
DateFormatter(java.lang.String inputSeparator)
          Constructor which provides for an alternate string separator.
 
Method Summary
 java.lang.String convert(java.lang.String date)
          Convert a date into a four-digit year form using a default output separator.
 java.lang.String convert(java.lang.String date, java.lang.String outputSeparator)
          Convert a date into a four-digit year form using the output outputSeparator supplied.
protected static java.lang.String convertMonthOrDay(java.lang.String field)
          Convert a month or day into a two-digit string
protected  java.lang.String convertYear(java.lang.String yearField)
          Convert a two- or four-digit year into a four-digit year
protected static java.util.regex.Pattern mmddyyPattern(java.lang.String inputSeparator)
          Construct MM-DD-YY pattern based upon the input separator.
protected static java.util.regex.Pattern mmyyPattern(java.lang.String inputSeparator)
          Construct MM-YY pattern based upon input separator
protected static java.lang.String zeroFilled(int value, int width)
          Return an integer value as a string, left-filled to required width with zeroes.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

defaultSeparator

private static final java.lang.String defaultSeparator
Default string which separates input dates.

See Also:
Constant Field Values

monthDay

private static final java.lang.String monthDay
Month and day pattern: one or two digits.

See Also:
Constant Field Values

twoDigitYear

private static final java.lang.String twoDigitYear
Two-digit year pattern: exactly two digits.

See Also:
Constant Field Values

fourDigitYear

private static final java.lang.String fourDigitYear
Four-digit year pattern: exactly four digits.

See Also:
Constant Field Values

epoch

private final DateFormatter.Epoch epoch
The DateFormatter.Epoch in which to interpret two-digit years


monthDayYear

private final java.util.regex.Pattern monthDayYear
Month-day-year pattern, compiled with given input separator.


monthYear

private final java.util.regex.Pattern monthYear
Month-year pattern, compiled with given input separator.


year

private final java.util.regex.Pattern year
Year pattern: two or four digits, no separator.

Constructor Detail

DateFormatter

public DateFormatter()
Default constructor. Assume that the "/" character is a separator, and that two-digit dates fall within fifty years of today's date.


DateFormatter

public DateFormatter(java.lang.String inputSeparator)
Constructor which provides for an alternate string separator.

Parameters:
inputSeparator - String that separates days, months, and years

DateFormatter

public DateFormatter(int firstYearInEpoch,
                     java.lang.String inputSeparator)
Constructor which provides for alternative separator and epoch.

Parameters:
inputSeparator - String that separates days, months, and years
firstYearInEpoch - add to two digit years to convert to real years
Method Detail

mmddyyPattern

protected static java.util.regex.Pattern mmddyyPattern(java.lang.String inputSeparator)
Construct MM-DD-YY pattern based upon the input separator.

Parameters:
inputSeparator - input date separator character
Returns:
Pattern to match mm-dd-yy or mm-dd-yyyy

mmyyPattern

protected static java.util.regex.Pattern mmyyPattern(java.lang.String inputSeparator)
Construct MM-YY pattern based upon input separator

Parameters:
inputSeparator - input date separator character
Returns:
Pattern to match mm-yy or mm-yyyy

zeroFilled

protected static java.lang.String zeroFilled(int value,
                                             int width)
Return an integer value as a string, left-filled to required width with zeroes.

Parameters:
value - Integer value to be converted to String
width - Width of output field desired
Returns:
String form of value, left filled with zeroes to width

convertMonthOrDay

protected static java.lang.String convertMonthOrDay(java.lang.String field)
Convert a month or day into a two-digit string

Parameters:
field - value of month or day field as String
Returns:
two-digit String value with leading zeroes.

convertYear

protected java.lang.String convertYear(java.lang.String yearField)
Convert a two- or four-digit year into a four-digit year

Parameters:
yearField - year value from host application
Returns:
String four-digit year

convert

public java.lang.String convert(java.lang.String date,
                                java.lang.String outputSeparator)
Convert a date into a four-digit year form using the output outputSeparator supplied. Dates consisting of one field are assumed to be a year, two fields are a month and a year, and three fields are a month, day, and year.

Parameters:
date - date as taken from the terminal screen
outputSeparator - String used to separate result fields
Returns:
four-year date or null if invalid format

convert

public java.lang.String convert(java.lang.String date)
Convert a date into a four-digit year form using a default output separator.

Parameters:
date - date as taken from the terminal screen
Returns:
four-year date or null if invalid format