package cnp.ew.converter;
import java.util.*;

/**
 * Used to display the hour portion of a date, using a prespecified
 * format. It is typically used as a component in converting an hour
 * to a full string with a prespecified format, where the hour output
 * is just one portion of the string.  Example usage:
 * <pre>
 *    CpHourToString converter = new CpDayToString("hh");
 *    convertedString = converter.convertDate(new Date("3/1/95 11:30"));
 * </pre>
 *
 * @see            cnp.ew.util.CpDateToStringConverter
 * @version        $Version$
 * @author         $Author: Ken $
 */
public class CpHourToString extends CpDateUnitToString
{

    boolean is12HourClock=false;

    /**
     * Creates a new converter, with the given format for conversion.
     * @param formatString  Possible formats are:
     *      "h"    hour of the day in one or two digits, as needed (0 to 23)
     *      "hh"   hour of the day in two digits (00 to 23).
     */
    public CpHourToString(String formatString)
    {
        super(formatString);
    }

    /**
     * Set whether the hours should display in 12 or 24 hour format.
     */
    public void is12HourClock(boolean bool)
    {
        is12HourClock = bool;
    }

    /**
     * Converts a given date to the appropriate string for the
     * predefined format.
     */
    public String convertDate(Date d)
    {
        int hours = d.getHours();
        if (is12HourClock && hours > 12) {
            hours -= 12;
        }

        if (is12HourClock && hours == 0) {
            hours = 12;
        }

        if (formatStyle == 1) {
            return "" + hours;
        } else {
            return prefixWithZero(hours);
        }
    }
}
