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