package cnp.ew.converter;
import java.util.*;
import cnp.ew.util.*;

/**
 * Used to display the year 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 year output
 * is just one portion of the string.  Example usage:
 * <pre>
 *    CpYearToString converter = new CpYearToString("yyyy");
 *    convertedString = converter.convertDate(new Date("3/1/95 11:30"));
 * </pre>
 *
 * @see            cnp.ew.util.CpDateToStringConverter
 * @version        $Version$
 * @author         $Author: Ken $
 */
public class CpYearToString extends CpDateUnitToString
{
    /**
     * Creates a new converter, with the given format for conversion.
     * @param formatString  Possible formats are:
     *      "y"    number of the day of the year (1 to 366).
     *      "yy"   the last two digits of the year (01 to 99).
     *      "yyyy" the full year (1900 to 9999).
     */
    public CpYearToString(String formatString)
    {
        super(formatString);
    }

    /**
     * Converts a given date to the appropriate string for the
     * predefined format.
     */
    public String convertDate(Date d)
    {
        switch (formatStyle) {
        case 1:
            return "" + (new CpDate(d)).getDayOfYear();
        case 2:
            return "" + d.getYear();
        default:
            return "" + (1900 + d.getYear());
        }
    }
}

