package cnp.ew.converter;
import java.util.*;
import cnp.ew.util.*;

/**
 * Used to display the week portion of a date, using a prespecified
 * format. It is typically used as a component in converting a week
 * to a full string with a prespecified format, where the week output
 * is just one portion of the string.  Example usage:
 * <pre>
 *    CpWeekToString converter = new CpWeekToString("ww");
 *    convertedString = converter.convertDate(new Date("3/1/95 11:30"));
 * </pre>
 *
 * @see            cnp.ew.util.CpDateToStringConverter
 * @version        $Version$
 * @author         $Author: Ken $
 */
class CpWeekToString extends CpDateUnitToString
{
    /**
     * Creates a new converter, with the given format for conversion.
     * @param formatString  Possible formats are:
     *      "w"    day of the week in digits (1 to 7)
     *      "ww"   week of the month in digits (1 to 53).
     */
    public CpWeekToString(String formatString)
    {
        super(formatString);
    }

    /**
     * Converts a given date to the appropriate string for the
     * predefined format.
     */
    public String convertDate(Date d)
    {

        int dayOfYear, weekOfYear;

        // 'w' - answer the day of the week (1 = Sunday)
        if (formatStyle == 1) {
            return "" + (d.getDay() + 1);

        // 'ww' - answer the week of the year (1 to 53)
        } else {
            dayOfYear = (new CpDate(d)).getDayOfYear();
            weekOfYear = (dayOfYear / 7) + 1 ;
            return "" + weekOfYear;
        }
    }
}
