com.ccg.util
Class DateFormatMulti

java.lang.Object
  extended by java.text.Format
      extended by java.text.DateFormat
          extended by com.ccg.util.DateFormatMulti
All Implemented Interfaces:
Serializable, Cloneable

public class DateFormatMulti
extends DateFormat

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");
       }
     }
   }
 }
 

Since:
1.0
Version:
$Revision: 1.3 $
Author:
$Author: pkb $
See Also:
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

DateFormatMulti

public DateFormatMulti()
Default constructor for the object.

You will most likely want to add one or more addDateFormats immediately after construction.

Since:
1.0
Method Detail

addDateFormat

public void addDateFormat(DateFormat df)
Add a 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.

Parameters:
df - DateFormat to add - must not be null.
Throws:
NullPointerException - If you pass null.
Since:
1.0

addDateFormat

public void addDateFormat(String dfs)
Add a "simple" 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.

Parameters:
dfs - String representation of date format. See SimpleDateFormat for details.
Throws:
NullPointerException - If you pass null.
Since:
1.0

format

public StringBuffer format(Date date,
                           StringBuffer sb,
                           FieldPosition fp)
Formats the Date into the primary format.

This method uses the first DateFormat which has been added to the collection to format the date object you pass.

Specified by:
format in class DateFormat
Parameters:
date - The date to be formatted.
sb - The StringBuffer to format the output into.
pos - The position in the StringBuffer to format the output into.
Returns:
String representation of the object.
Throws:
IllegalStateException - If you have not added any valid date formats to the collection yet.
Since:
1.0

parse

public Date parse(String text,
                  ParsePosition pp)
Attempts to parse a 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).

Specified by:
parse in class DateFormat
Parameters:
text - String to parse Date from.
pp - Parse position within text.
Returns:
A Date corresponding to the text or null if unable to parse.
Since:
1.0

setCalendar

public void setCalendar(Calendar val)
Set the Calendar associated with the formatter.

Overrides:
setCalendar in class DateFormat
Parameters:
val - New Calendar value to assign.
See Also:
getCalendar()

getCalendar

public Calendar getCalendar()
Get the Calendar associated with the formatter.

Overrides:
getCalendar in class DateFormat
Returns:
Current Calendar value assigned.
See Also:
setCalendar(java.util.Calendar)

setNumberFormat

public void setNumberFormat(NumberFormat val)
Set the NumberFormat associated with the formatter.

Overrides:
setNumberFormat in class DateFormat
Parameters:
val - New NumberFormat value to assign.
See Also:
getNumberFormat()

getNumberFormat

public NumberFormat getNumberFormat()
Get the NumberFormat associated with the formatter.

Overrides:
getNumberFormat in class DateFormat
Returns:
Current NumberFormat value assigned.
See Also:
setNumberFormat(java.text.NumberFormat)

setTimeZone

public void setTimeZone(TimeZone val)
Set the TimeZone associated with the formatter.

Overrides:
setTimeZone in class DateFormat
Parameters:
val - New TimeZone value to assign.
See Also:
getTimeZone()

getTimeZone

public TimeZone getTimeZone()
Get the TimeZone associated with the formatter.

Overrides:
getTimeZone in class DateFormat
Returns:
Current TimeZone value assigned.
See Also:
setTimeZone(java.util.TimeZone)

setLenient

public void setLenient(boolean val)
Set whether to use lenient parsing rules.

Overrides:
setLenient in class DateFormat
Parameters:
val - Pass true to make parse less strict (more likely to succeed).

isLenient

public boolean isLenient()
Get the Lenient associated with the formatter.

Overrides:
isLenient in class DateFormat
Returns:
true if lenient parsing rules are in affect.
See Also:
setLenient(boolean)

toString

public String toString()
Simple string representation of the object.

Overrides:
toString in class Object
Returns:
String representation of the primary format.
Since:
1.0

hashCode

public int hashCode()
Get a hash code for the object.

Overrides:
hashCode in class DateFormat
Returns:
Integer value to use as hash code for object.
Since:
1.0

equals

public boolean equals(Object o)
Determine if this object is equal to another object.

Overrides:
equals in class DateFormat
Parameters:
obj - Other object to compare to.
Returns:
true if other object is reference to this object, false if not.
Since:
1.0


Copyright 1998-1998-2006 null. All Rights Reserved.