예제 #1
0
        /// <summary>
        /// Gets an iterator of text to field for the specified chrono, field, locale and style
        /// for the purpose of parsing.
        /// <para>
        /// The iterator must be returned in order from the longest text to the shortest.
        /// </para>
        /// <para>
        /// The null return value should be used if there is no applicable parsable text, or
        /// if the text would be a numeric representation of the value.
        /// Text can only be parsed if all the values for that field-style-locale combination are unique.
        ///
        /// </para>
        /// </summary>
        /// <param name="chrono">  the Chronology to get text for, not null </param>
        /// <param name="field">  the field to get text for, not null </param>
        /// <param name="style">  the style to get text for, null for all parsable text </param>
        /// <param name="locale">  the locale to get text for, not null </param>
        /// <returns> the iterator of text to field pairs, in order from longest text to shortest text,
        ///  null if the field or style is not parsable </returns>
        public virtual IEnumerator <Map_Entry <String, Long> > GetTextIterator(Chronology chrono, TemporalField field, TextStyle style, Locale locale)
        {
            if (chrono == IsoChronology.INSTANCE || !(field is ChronoField))
            {
                return(GetTextIterator(field, style, locale));
            }

            int fieldIndex;

            switch ((ChronoField)field)
            {
            case ERA:
                fieldIndex = DateTime.ERA;
                break;

            case MONTH_OF_YEAR:
                fieldIndex = DateTime.MONTH;
                break;

            case DAY_OF_WEEK:
                fieldIndex = DateTime.DAY_OF_WEEK;
                break;

            case AMPM_OF_DAY:
                fieldIndex = DateTime.AM_PM;
                break;

            default:
                return(null);
            }

            int calendarStyle = (style == null) ? DateTime.ALL_STYLES : style.toCalendarStyle();
            IDictionary <String, Integer> map = CalendarDataUtility.retrieveJavaTimeFieldValueNames(chrono.CalendarType, fieldIndex, calendarStyle, locale);

            if (map == null)
            {
                return(null);
            }
            IList <Map_Entry <String, Long> > list = new List <Map_Entry <String, Long> >(map.Count);

            switch (fieldIndex)
            {
            case DateTime.ERA:
                foreach (Map_Entry <String, Integer> entry in map)
                {
                    int era = entry.Value;
                    if (chrono == JapaneseChronology.INSTANCE)
                    {
                        if (era == 0)
                        {
                            era = -999;
                        }
                        else
                        {
                            era -= 2;
                        }
                    }
                    list.Add(CreateEntry(entry.Key, (long)era));
                }
                break;

            case DateTime.MONTH:
                foreach (Map_Entry <String, Integer> entry in map)
                {
                    list.Add(CreateEntry(entry.Key, (long)(entry.Value + 1)));
                }
                break;

            case DateTime.DAY_OF_WEEK:
                foreach (Map_Entry <String, Integer> entry in map)
                {
                    list.Add(CreateEntry(entry.Key, (long)ToWeekDay(entry.Value)));
                }
                break;

            default:
                foreach (Map_Entry <String, Integer> entry in map)
                {
                    list.Add(CreateEntry(entry.Key, (long)entry.Value));
                }
                break;
            }
            return(list.GetEnumerator());
        }
예제 #2
0
        /// <summary>
        /// Gets the text for the specified chrono, field, locale and style
        /// for the purpose of formatting.
        /// <para>
        /// The text associated with the value is returned.
        /// The null return value should be used if there is no applicable text, or
        /// if the text would be a numeric representation of the value.
        ///
        /// </para>
        /// </summary>
        /// <param name="chrono">  the Chronology to get text for, not null </param>
        /// <param name="field">  the field to get text for, not null </param>
        /// <param name="value">  the field value to get text for, not null </param>
        /// <param name="style">  the style to get text for, not null </param>
        /// <param name="locale">  the locale to get text for, not null </param>
        /// <returns> the text for the field value, null if no text found </returns>
        public virtual String GetText(Chronology chrono, TemporalField field, long value, TextStyle style, Locale locale)
        {
            if (chrono == IsoChronology.INSTANCE || !(field is ChronoField))
            {
                return(GetText(field, value, style, locale));
            }

            int fieldIndex;
            int fieldValue;

            if (field == ERA)
            {
                fieldIndex = DateTime.ERA;
                if (chrono == JapaneseChronology.INSTANCE)
                {
                    if (value == -999)
                    {
                        fieldValue = 0;
                    }
                    else
                    {
                        fieldValue = (int)value + 2;
                    }
                }
                else
                {
                    fieldValue = (int)value;
                }
            }
            else if (field == MONTH_OF_YEAR)
            {
                fieldIndex = DateTime.MONTH;
                fieldValue = (int)value - 1;
            }
            else if (field == DAY_OF_WEEK)
            {
                fieldIndex = DateTime.DAY_OF_WEEK;
                fieldValue = (int)value + 1;
                if (fieldValue > 7)
                {
                    fieldValue = DayOfWeek.Sunday;
                }
            }
            else if (field == AMPM_OF_DAY)
            {
                fieldIndex = DateTime.AM_PM;
                fieldValue = (int)value;
            }
            else
            {
                return(null);
            }
            return(CalendarDataUtility.retrieveJavaTimeFieldValueName(chrono.CalendarType, fieldIndex, fieldValue, style.toCalendarStyle(), locale));
        }