|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectshared.formatting.DateFormatter
public class DateFormatter
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 |
---|
private static final java.lang.String defaultSeparator
private static final java.lang.String monthDay
private static final java.lang.String twoDigitYear
private static final java.lang.String fourDigitYear
private final DateFormatter.Epoch epoch
DateFormatter.Epoch
in which to interpret two-digit years
private final java.util.regex.Pattern monthDayYear
private final java.util.regex.Pattern monthYear
private final java.util.regex.Pattern year
Constructor Detail |
---|
public DateFormatter()
public DateFormatter(java.lang.String inputSeparator)
inputSeparator
- String that separates days, months, and yearspublic DateFormatter(int firstYearInEpoch, java.lang.String inputSeparator)
inputSeparator
- String that separates days, months, and yearsfirstYearInEpoch
- add to two digit years to convert to real yearsMethod Detail |
---|
protected static java.util.regex.Pattern mmddyyPattern(java.lang.String inputSeparator)
inputSeparator
- input date separator character
protected static java.util.regex.Pattern mmyyPattern(java.lang.String inputSeparator)
inputSeparator
- input date separator character
protected static java.lang.String zeroFilled(int value, int width)
value
- Integer value to be converted to Stringwidth
- Width of output field desired
protected static java.lang.String convertMonthOrDay(java.lang.String field)
field
- value of month or day field as String
protected java.lang.String convertYear(java.lang.String yearField)
yearField
- year value from host application
public java.lang.String convert(java.lang.String date, java.lang.String outputSeparator)
date
- date as taken from the terminal screenoutputSeparator
- String used to separate result fields
public java.lang.String convert(java.lang.String date)
date
- date as taken from the terminal screen
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |