Code example for FieldPosition

Methods: setBeginIndex, setEndIndex

0
        extends DateFormat {
    protected final String pattern = "HH";
 
    public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
        toAppendTo.append(StringUtil.leftPadding(2, '0', String.valueOf(date.getHours())));
        fieldPosition.setBeginIndex(0);
        fieldPosition.setEndIndex(2);
        return toAppendTo;
    } 
 
    public Date parse(String text, ParsePosition pos) {
        if (StringUtil.zero(text))
            return null; 
        if (text.length() != pattern.length())
            return null; 
        Date d = new Date(); 
        d.setYear(0); 
        d.setMonth(0); 
        d.setDate(0); 
        d.setHours(Integer.parseInt(text.substring(0, 2)));
        d.setMinutes(0);