package cnp.ew.converter;
import java.util.*;
import cnp.ew.util.*;

/**
 * Used to display the day portion of a date, using a prespecified
 * format. It is typically used as a component in converting a date
 * to a full string with a prespecified format, where the day output
 * is just one portion of the string.  Example usage:
 * <pre>
 *    CpDayToString converter = new CpDayToString("dddd");
 *    convertedString = converter.convertDate(new Date("3/1/95 11:30"));
 * </pre>
 *
 * @see            cnp.ew.util.CpDateToStringConverter
 * @version        $Version$
 * @author         $Author: Ken $
 */
public class CpDayToString extends CpDateUnitToString
{
    /**
     * Creates a new converter, with the given format for conversion.
     * @param formatString  Possible formats are:
     *      "d"    day of the month in one or two numeric digits, as needed (1 to 31)
     *      "dd"   day of the month in two numeric digits (01 to 31).
     *      "ddd"  first three letters of the weekday (Sun to Sat).
     *      "dddd" full name of the weekday (Sunday to Saturday).
     */
    public CpDayToString(String formatString)
    {
        super(formatString);
    }

    /**
     * Converts a given date to the appropriate string for the
     * predefined format.
     */
    public String convertDate(Date d)
    {
        switch (formatStyle) {

        // 'd' - numeric, no leading zero.
        case 1:
            return "" + d.getDate();

        // 'dd' - numeric with leading zero.
        case 2:
            return prefixWithZero(d.getDate());

        // 'ddd' - short string
        case 3:
            return (new CpDate(d)).getShortDayOfWeekName();

        // 'dddd' - long string
        case 4:
            return (new CpDate(d)).getLongDayOfWeekName();

        default:
        // This should never happen
            return "ERROR - day format length is " + formatStyle;
        }
    }
}
