Java Code Examples for java.text.CalendarBuilder

Following code examples demonstrate how to use java.text.CalendarBuilderfrom android. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use java.text.CalendarBuilderand various code implementation of this class.

        private int subParseNumericZone(String text, int start, int sign, int count, boolean colonRequired, CalendarBuilder calb) {
            int i = start;
            i = start + 1;
            try {
                char c = text.charAt(start);
                if (isDigit(c)) {
                    int hours = c - 48;
                    int index = i + 1;
                    try {
                        c = text.charAt(i);
                        if (isDigit(c)) {
                            hours = (hours * 10) + (c - 48);
                        } else {
                            index--;
                        }
                        if (hours > 23) {
                            i = index;
                        } else {
                            int minutes = 0;
                            if (count != 1) {
                                i = index + 1;
                                c = text.charAt(index);
                                if (c == ':') {
                                    index = i + 1;
                                    c = text.charAt(i);
                                } else if (!colonRequired) {
                                    index = i;
                                }
                                if (isDigit(c)) {
                                    minutes = c - 48;
                                    i = index + 1;
                                    c = text.charAt(index);
                                    if (isDigit(c)) {
                                        minutes = (minutes * 10) + (c - 48);
                                    }
                                } else {
                                    i = index;
                                }
                            } else {
                                i = index;
                            }
                            calb.set(15, (MILLIS_PER_MINUTE * (minutes + (hours * 60))) * sign).set(16, 0);
                            return i;
                        }
                    } catch (IndexOutOfBoundsException e) {
                        i = index;
                    }
                }
            } catch (IndexOutOfBoundsException e2) {
            }
            return 1 - i;
        } 


        private int subParseZoneString(String text, int start, CalendarBuilder calb) {
            if (this.formatData.isZoneStringsSet) {
                return subParseZoneStringFromSymbols(text, start, calb);
            }
            return subParseZoneStringFromICU(text, start, calb);
        } 

        private int parseWeekday(String text, int start, int field, boolean useDateFormatSymbols, boolean standalone, CalendarBuilder out) {
            int index = -1;
            if (useDateFormatSymbols) {
                index = matchString(text, start, 7, standalone ? this.formatData.getStandAloneWeekdays() : this.formatData.getWeekdays(), out);
                if (index > 0) {
                    return index;
                }
                index = matchString(text, start, 7, standalone ? this.formatData.getShortStandAloneWeekdays() : this.formatData.getShortWeekdays(), out);
                if (index > 0) {
                    return index;
                }
            }
            for (int style : new int[]{2, 1}) {
                index = matchString(text, start, field, this.calendar.getDisplayNames(field, style, this.locale), out);
                if (index > 0) {
                    return index;
                }
            } 

        private int subParseZoneStringFromICU(String text, int start, CalendarBuilder calb) {
            String tzId;
            ULocale uLocale;
            String region;
            TimeZone newTimeZone;
            boolean isDst;
            int dstAmount;
            String currentTimeZoneID = android.icu.util.TimeZone.getCanonicalID(getTimeZone().getID());
            TimeZoneNames tzNames = getTimeZoneNames();
            MatchInfo matchInfo = null;
            Set set = null;
            if (UTC.length() + start <= text.length()) {
                if (text.regionMatches(true, start, UTC, 0, UTC.length())) {
                    matchInfo = new MatchInfo(NameType.SHORT_GENERIC, UTC, null, UTC.length());
                    tzId = matchInfo.tzID();
                    if (tzId == null) {
                        if (set == null) {
                            set = tzNames.getAvailableMetaZoneIDs(currentTimeZoneID);
                        }
                        if (set.contains(matchInfo.mzID())) {
                            uLocale = ULocale.forLocale(this.locale);
                            region = uLocale.getCountry();
                            if (region.length() == 0) {
                                region = ULocale.addLikelySubtags(uLocale).getCountry();
                            }
                            tzId = tzNames.getReferenceZoneID(matchInfo.mzID(), region);
                        } else {
                            tzId = currentTimeZoneID;
                        }
                    }
                    newTimeZone = TimeZone.getTimeZone(tzId);
                    if (!currentTimeZoneID.equals(tzId)) {
                        setTimeZone(newTimeZone);
                    }
                    isDst = DST_NAME_TYPES.contains(matchInfo.nameType());
                    dstAmount = isDst ? newTimeZone.getDSTSavings() : 0;
                    if (!(isDst && dstAmount == 0)) {
                        calb.clear(15).set(16, dstAmount);
                    }
                    return matchInfo.matchLength() + start;
                }
            }
            for (MatchInfo match : tzNames.find(text, start, NAME_TYPES)) {
                if (matchInfo == null || matchInfo.matchLength() < match.matchLength()) {
                    matchInfo = match;
                } else if (matchInfo.matchLength() != match.matchLength()) {
                    continue;
                } else if (currentTimeZoneID.equals(match.tzID())) {
                    matchInfo = match;
                    break;
                } else if (match.mzID() == null) {
                    continue;
                } else {
                    if (set == null) {
                        set = tzNames.getAvailableMetaZoneIDs(currentTimeZoneID);
                    }
                    if (set.contains(match.mzID())) {
                        matchInfo = match;
                        break;
                    }
                }
            }
            if (matchInfo == null) {
                return -start;
            }
            tzId = matchInfo.tzID();
            if (tzId == null) {
                if (set == null) {
                    set = tzNames.getAvailableMetaZoneIDs(currentTimeZoneID);
                }
                if (set.contains(matchInfo.mzID())) {
                    uLocale = ULocale.forLocale(this.locale);
                    region = uLocale.getCountry();
                    if (region.length() == 0) {
                        region = ULocale.addLikelySubtags(uLocale).getCountry();
                    }
                    tzId = tzNames.getReferenceZoneID(matchInfo.mzID(), region);
                } else {
                    tzId = currentTimeZoneID;
                }
            }
            newTimeZone = TimeZone.getTimeZone(tzId);
            if (currentTimeZoneID.equals(tzId)) {
                setTimeZone(newTimeZone);
            }
            isDst = DST_NAME_TYPES.contains(matchInfo.nameType());
            if (isDst) {
            }
            calb.clear(15).set(16, dstAmount);
            return matchInfo.matchLength() + start;
        } 

    private Date parseInternal(String text, ParsePosition pos) {
        checkNegativeNumberExpression();
        int start = pos.index;
        int oldStart = start;
        int textLength = text.length();
        boolean[] ambiguousYear = new boolean[]{-assertionsDisabled};
        CalendarBuilder calb = new CalendarBuilder();
        int i = 0;
        while (i < this.compiledPattern.length) {
            int tag = this.compiledPattern[i] >>> 8;
            int i2 = i + 1;
            int count = this.compiledPattern[i] & 255;
            if (count == 255) {
                i = i2 + 1;
                count = (this.compiledPattern[i2] << 16) | this.compiledPattern[i];
                i++;
            } else {
                i = i2;
            }
            switch (tag) {
                case TAG_QUOTE_ASCII_CHAR :
                    if (start < textLength && text.charAt(start) == ((char) count)) {
                        start++;
                        break;
                    }
                    pos.index = oldStart;
                    pos.errorIndex = start;
                    return null;
                    break;
                case TAG_QUOTE_CHARS :
                    int count2 = count;
                    i2 = i;
                    while (true) {
                        count = count2 - 1;
                        if (count2 <= 0) {
                            i = i2;
                            break;
                        }
                        if (start < textLength) {
                            i = i2 + 1;
                            if (text.charAt(start) == this.compiledPattern[i2]) {
                                start++;
                                count2 = count;
                                i2 = i;
                            }
                        }
                        pos.index = oldStart;
                        pos.errorIndex = start;
                        return null;
                    }
                default:
                    boolean obeyCount = -assertionsDisabled;
                    boolean useFollowingMinusSignAsDelimiter = -assertionsDisabled;
                    if (i < this.compiledPattern.length) {
                        int nextTag = this.compiledPattern[i] >>> 8;
                        if (!(nextTag == TAG_QUOTE_ASCII_CHAR || nextTag == TAG_QUOTE_CHARS)) {
                            obeyCount = true;
                        }
                        if (this.hasFollowingMinusSign && (nextTag == TAG_QUOTE_ASCII_CHAR || nextTag == TAG_QUOTE_CHARS)) {
                            char c;
                            if (nextTag == TAG_QUOTE_ASCII_CHAR) {
                                c = this.compiledPattern[i] & 255;
                            } else {
                                c = this.compiledPattern[i + 1];
                            }
                            if (c == this.minusSign) {
                                useFollowingMinusSignAsDelimiter = true;
                            }
                        }
                    }
                    start = subParse(text, start, tag, count, obeyCount, ambiguousYear, pos, useFollowingMinusSignAsDelimiter, calb);
                    if (start >= 0) {
                        break;
                    }
                    pos.index = oldStart;
                    return null;
            }
        }
        pos.index = start;
        try {
            Date parsedDate = calb.establish(this.calendar).getTime();
            if (ambiguousYear[0]) {
                if (parsedDate.before(this.defaultCenturyStart)) {
                    parsedDate = calb.addYear(TAG_QUOTE_ASCII_CHAR).establish(this.calendar).getTime();
                }
            }
            return parsedDate;
        } catch (IllegalArgumentException e) {
            pos.errorIndex = start;
            pos.index = oldStart;
            return null;
        }
    } 

        private int parseMonth(String text, int count, int value, int start, int field, ParsePosition pos, boolean useDateFormatSymbols, boolean standalone, CalendarBuilder out) {
            if (count <= 2) {
                out.set(2, value - 1);
                return pos.index;
            }
            int index;
            if (useDateFormatSymbols) {
                index = matchString(text, start, 2, standalone ? this.formatData.getStandAloneMonths() : this.formatData.getMonths(), out);
                if (index > 0) {
                    return index;
                }
                index = matchString(text, start, 2, standalone ? this.formatData.getShortStandAloneMonths() : this.formatData.getShortMonths(), out);
                if (index > 0) {
                    return index;
                }
            }
            index = matchString(text, start, field, this.calendar.getDisplayNames(field, 0, this.locale), out);
            if (index > 0) {
                return index;
            }
            return index;
        } 

        private int subParseZoneStringFromSymbols(String text, int start, CalendarBuilder calb) {
            boolean z = -assertionsDisabled;
            TimeZone currentTimeZone = getTimeZone();
            int zoneIndex = this.formatData.getZoneIndex(currentTimeZone.getID());
            TimeZone tz = null;
            String[][] zoneStrings = this.formatData.getZoneStringsWrapper();
            String[] strArr = null;
            int nameIndex = 0;
            if (zoneIndex != -1) {
                strArr = zoneStrings[zoneIndex];
                nameIndex = matchZoneString(text, start, strArr);
                if (nameIndex > 0) {
                    if (nameIndex <= 2) {
                        z = strArr[nameIndex].equalsIgnoreCase(strArr[nameIndex + 2]);
                    }
                    tz = TimeZone.getTimeZone(strArr[0]);
                }
            }
            if (tz == null) {
                zoneIndex = this.formatData.getZoneIndex(TimeZone.getDefault().getID());
                if (zoneIndex != -1) {
                    strArr = zoneStrings[zoneIndex];
                    nameIndex = matchZoneString(text, start, strArr);
                    if (nameIndex > 0) {
                        if (nameIndex <= 2) {
                            z = strArr[nameIndex].equalsIgnoreCase(strArr[nameIndex + 2]);
                        }
                        tz = TimeZone.getTimeZone(strArr[0]);
                    }
                }
            }
            if (tz == null) {
                int len = zoneStrings.length;
                int i = 0;
                while (i < len) {
                    strArr = zoneStrings[i];
                    nameIndex = matchZoneString(text, start, strArr);
                    if (nameIndex > 0) {
                        if (nameIndex <= 2) {
                            z = strArr[nameIndex].equalsIgnoreCase(strArr[nameIndex + 2]);
                        }
                        tz = TimeZone.getTimeZone(strArr[0]);
                    } else {
                        i++;
                    }
                }
            }
            if (tz == null) {
                return -start;
            }
            if (!tz.equals(currentTimeZone)) {
                setTimeZone(tz);
            }
            int dstAmount = nameIndex >= 3 ? tz.getDSTSavings() : 0;
            Object obj = (z || (nameIndex >= 3 && dstAmount == 0)) ? 1 : null;
            if (obj == null) {
                calb.clear(15).set(16, dstAmount);
            }
            return strArr[nameIndex].length() + start;
        } 

        private int matchString(String text, int start, int field, Map<String, Integer> data, CalendarBuilder calb) {
            if (data != null) {
                String bestMatch = null;
                for (String name : data.keySet()) {
                    int length = name.length();
                    if ((bestMatch == null || length > bestMatch.length()) && text.regionMatches(true, start, name, 0, length)) {
                        bestMatch = name;
                    }
                }
                if (bestMatch != null) {
                    calb.set(field, ((Integer) data.get(bestMatch)).intValue());
                    return bestMatch.length() + start;
                }
            }
            return -start;
        } 

Advertisement
Javadoc
CalendarBuilder keeps field-value pairs for setting the calendar fields of the given Calendar. It ha

s the Calendar#FIELD_COUNT FIELD_COUNT-th field for the week year support. Also ISO_DAY_OF_WEEK is used to specify DAY_OF_WEEK in the ISO day of week numbering.

CalendarBuilder retains the semantic of the pseudo timestamp for fields. CalendarBuilder uses a single int array combining fields[] and stamp[] of Calendar. @author Masayoshi Okutsu

Read More
Advertisement