Code example for DateFormat

Methods: clone, format, setTimeZone

0
            FieldPosition fieldPosition)
    { 
        if (_formatISO8601 == null) {
            _formatISO8601 = _cloneFormat(DATE_FORMAT_ISO8601);
        } 
        return _formatISO8601.format(date, toAppendTo, fieldPosition);
    } 
 
    /* 
    /********************************************************** 
    /* Helper methods 
    /********************************************************** 
     */ 
 
    /** 
     * Overridable helper method used to figure out which of supported 
     * formats is the likeliest match. 
     */ 
    protected boolean looksLikeISO8601(String dateStr)
    { 
        if (dateStr.length() >= 5
            && Character.isDigit(dateStr.charAt(0))
            && Character.isDigit(dateStr.charAt(3))
            && dateStr.charAt(4) == '-'
            ) { 
            return true; 
        } 
        return false; 
    } 
 
    protected Date parseAsISO8601(String dateStr, ParsePosition pos)
    { 
        /* 21-May-2009, tatu: DateFormat has very strict handling of 
         * timezone  modifiers for ISO-8601. So we need to do some scrubbing. 
         */ 
 
        /* First: do we have "zulu" format ('Z' == "GMT")? If yes, that's 
         * quite simple because we already set date format timezone to be 
         * GMT, and hence can just strip out 'Z' altogether 
         */ 
        int len = dateStr.length();
        char c = dateStr.charAt(len-1);
        DateFormat df;
 
        // [JACKSON-200]: need to support "plain" date... 
        if (len <= 10 && Character.isDigit(c)) {
           df = _formatPlain;
            if (df == null) {
                df = _formatPlain = _cloneFormat(DATE_FORMAT_PLAIN);
            } 
        } else if (c == 'Z') {
            df = _formatISO8601_z;
            if (df == null) {
                df = _formatISO8601_z = _cloneFormat(DATE_FORMAT_ISO8601_Z);
            } 
            // [JACKSON-334]: may be missing milliseconds... if so, add 
            if (dateStr.charAt(len-4) == ':') {
                StringBuilder sb = new StringBuilder(dateStr);
                sb.insert(len-1, ".000");
                dateStr = sb.toString();
            } 
        } else { 
            // Let's see if we have timezone indicator or not... 
            if (hasTimeZone(dateStr)) {
                c = dateStr.charAt(len-3);
                if (c == ':') { // remove optional colon
                    // remove colon 
                    StringBuilder sb = new StringBuilder(dateStr);
                    sb.delete(len-3, len-2);
                    dateStr = sb.toString();
                } else if (c == '+' || c == '-') { // missing minutes
                    // let's just append '00' 
                    dateStr += "00";
                } 
                // [JACKSON-334]: may be missing milliseconds... if so, add 
                len = dateStr.length();
                // '+0000' (5 chars); should come after '.000' (4 chars) of milliseconds, so: 
                c = dateStr.charAt(len-9);
                if (Character.isDigit(c)) {
                    StringBuilder sb = new StringBuilder(dateStr);
                    sb.insert(len-5, ".000");
                    dateStr = sb.toString();
                } 
                 
                df = _formatISO8601;
                if (_formatISO8601 == null) {
                    df = _formatISO8601 = _cloneFormat(DATE_FORMAT_ISO8601);
                } 
            } else { 
                /* 24-Nov-2009, tatu: Ugh. This is getting pretty 
                 *   ugly. Need to rewrite soon! 
                 */ 
 
                // If not, plain date. Easiest to just patch 'Z' in the end? 
                StringBuilder sb = new StringBuilder(dateStr);
                // And possible also millisecond part if missing 
                int timeLen = len - dateStr.lastIndexOf('T') - 1;
                if (timeLen <= 8) {
                    sb.append(".000");
                } 
                sb.append('Z');
                dateStr = sb.toString();
                df = _formatISO8601_z;
                if (df == null) {
                    df = _formatISO8601_z = _cloneFormat(DATE_FORMAT_ISO8601_Z);
                } 
            } 
        } 
        return df.parse(dateStr, pos);
    } 
 
    protected Date parseAsRFC1123(String dateStr, ParsePosition pos)
    { 
        if (_formatRFC1123 == null) {
            _formatRFC1123 = _cloneFormat(DATE_FORMAT_RFC1123);
        } 
        return _formatRFC1123.parse(dateStr, pos);
    } 
 
    private final static boolean hasTimeZone(String str)
    { 
        // Only accept "+hh", "+hhmm" and "+hh:mm" (and with minus), so 
        int len = str.length();
        if (len >= 6) {
            char c = str.charAt(len-6);
            if (c == '+' || c == '-') return true;
            c = str.charAt(len-5);
            if (c == '+' || c == '-') return true;
            c = str.charAt(len-3);
            if (c == '+' || c == '-') return true;
        } 
        return false; 
    } 
 
    private final DateFormat _cloneFormat(DateFormat df) {
        return _cloneFormat(df, _timezone);
    } 
 
    private final static DateFormat _cloneFormat(DateFormat df, TimeZone tz)
    { 
        df = (DateFormat) df.clone();
        if (tz != null) {
            df.setTimeZone(tz);
        } 
        return df;
    } 
}