|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjava.text.Format
java.text.DateFormat
com.ccg.util.DateFormatMulti
public class DateFormatMulti
Allows one to "join" multiple DateFormat
objects into a single DateFormat.
This class deals with the parsing and formatting of Date objects. In particular:
IMPORTANT NOTE: one of the first things you will want to
do is add a date format to this "collection" of date
formats. If you invoke a DateFormat method (such as format(java.util.Date, java.lang.StringBuffer, java.text.FieldPosition), setTimeZone(java.util.TimeZone), etc) prior to adding a default date
format, one will be created for you.
import com.ccg.util.DateFormatMulti;
class Example {
public void main(String[] args) {
DateFormatMulti dfm = new DateFormatMulti();
dfm.addDateFormat(new java.text.SimpleDateFormat("yyyy-MM-dd"));
dfm.addDateFormat("MM/dd");
for (int i = 0; i < args.length; i++) {
try {
System.out.println(dfm.parseObject(args[i]));
} catch (Exception e) {
System.err(args[i]+" is not in a recognized form");
}
}
}
}
SimpleDateFormat,
Serialized Form| Nested Class Summary |
|---|
| Nested classes/interfaces inherited from class java.text.DateFormat |
|---|
DateFormat.Field |
| Field Summary |
|---|
| 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 | |
|---|---|
DateFormatMulti()
Default constructor for the object. |
|
| Method Summary | |
|---|---|
void |
addDateFormat(DateFormat df)
Add a DateFormat to the list of recognized formats. |
void |
addDateFormat(String dfs)
Add a "simple" DateFormat to the list of recognized formats. |
boolean |
equals(Object o)
Determine if this object is equal to another object. |
StringBuffer |
format(Date date,
StringBuffer sb,
FieldPosition fp)
Formats the Date into the primary format. |
Calendar |
getCalendar()
Get the Calendar associated with the formatter. |
NumberFormat |
getNumberFormat()
Get the NumberFormat associated with the formatter. |
TimeZone |
getTimeZone()
Get the TimeZone associated with the formatter. |
int |
hashCode()
Get a hash code for the object. |
boolean |
isLenient()
Get the Lenient associated with the formatter. |
Date |
parse(String text,
ParsePosition pp)
Attempts to parse a Date from some text. |
void |
setCalendar(Calendar val)
Set the Calendar associated with the formatter. |
void |
setLenient(boolean val)
Set whether to use lenient parsing rules. |
void |
setNumberFormat(NumberFormat val)
Set the NumberFormat associated with the formatter. |
void |
setTimeZone(TimeZone val)
Set the TimeZone associated with the formatter. |
String |
toString()
Simple string representation of the object. |
| Methods inherited from class java.text.DateFormat |
|---|
clone, format, format, getAvailableLocales, getDateInstance, getDateInstance, getDateInstance, getDateTimeInstance, getDateTimeInstance, getDateTimeInstance, getInstance, getTimeInstance, getTimeInstance, getTimeInstance, parse, parseObject |
| Methods inherited from class java.text.Format |
|---|
format, formatToCharacterIterator, parseObject |
| Methods inherited from class java.lang.Object |
|---|
finalize, getClass, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
|---|
public DateFormatMulti()
You will most likely want to add one or more addDateFormats immediately after
construction.
| Method Detail |
|---|
public void addDateFormat(DateFormat df)
DateFormat to the list of recognized formats.
This method is used to add a new date format to the list of recognized formats. If it is the first date format added, it will become the output format for the object.
df - DateFormat to add - must not be null.
NullPointerException - If you pass null.public void addDateFormat(String dfs)
DateFormat to the list of recognized formats.
This method is used to add a new date format using a simple string template (like: "yyyy-MM-dd HH:mm:ss"). If it is the first date format added, it will become the output format for the object.
dfs - String representation of date format. See SimpleDateFormat for details.
NullPointerException - If you pass null.
public StringBuffer format(Date date,
StringBuffer sb,
FieldPosition fp)
This method uses the first DateFormat which
has been added to the collection to format the date object you
pass.
format in class DateFormatdate - The date to be formatted.sb - The StringBuffer to format the output into.pos - The position in the StringBuffer to format the output into.
IllegalStateException - If you have not added any valid date formats to the collection yet.
public Date parse(String text,
ParsePosition pp)
Date from some text.
This method iterates through all of the DateFormat(s) available until it finds one capable of parsing the text passed or until we run out of DateFormat(s).
parse in class DateFormattext - String to parse Date from.pp - Parse position within text.
Date corresponding to the text or null if
unable to parse.public void setCalendar(Calendar val)
setCalendar in class DateFormatval - New Calendar value to assign.getCalendar()public Calendar getCalendar()
getCalendar in class DateFormatsetCalendar(java.util.Calendar)public void setNumberFormat(NumberFormat val)
setNumberFormat in class DateFormatval - New NumberFormat value to assign.getNumberFormat()public NumberFormat getNumberFormat()
getNumberFormat in class DateFormatsetNumberFormat(java.text.NumberFormat)public void setTimeZone(TimeZone val)
setTimeZone in class DateFormatval - New TimeZone value to assign.getTimeZone()public TimeZone getTimeZone()
getTimeZone in class DateFormatsetTimeZone(java.util.TimeZone)public void setLenient(boolean val)
setLenient in class DateFormatval - Pass true to make parse less strict (more likely to succeed).public boolean isLenient()
isLenient in class DateFormatsetLenient(boolean)public String toString()
toString in class Objectpublic int hashCode()
hashCode in class DateFormatpublic boolean equals(Object o)
equals in class DateFormatobj - Other object to compare to.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||