Date interval means from one date to another date, for example, from "Jan 11, 2008" to "Jan 18, 2008". We introduced class DateInterval to represent it. DateInterval is a pair of UDate, which is the standard milliseconds since 24:00 GMT, Jan 1, 1970.
DateIntervalFormat formats a DateInterval into text as compactly as possible. For example, the date interval format from "Jan 11, 2008" to "Jan 18,. 2008" is "Jan 11-18, 2008" for English. And it parses text into DateInterval, although initially, parsing is not supported.
There is no structural information in date time patterns. For any punctuations and string literals inside a date time pattern, we do not know whether it is just a separator, or a prefix, or a suffix. Without such information, so, it is difficult to generate a sub-pattern (or super-pattern) by algorithm. So, formatting a DateInterval is pattern-driven. It is very similar to formatting in SimpleDateFormat. We introduce class DateIntervalInfo to save date interval patterns, similar to date time pattern in SimpleDateFormat.
Logically, the interval patterns are mappings from (skeleton, the_largest_different_calendar_field) to (date_interval_pattern).
A skeleton
The calendar fields we support for interval formatting are: year, month, date, day-of-week, am-pm, hour, hour-of-day, minute, and second (though we do not currently have specific intervalFormat data for skeletons with seconds). Those calendar fields can be defined in the following order: year > month > date > hour (in day) > minute > second The largest different calendar fields between 2 calendars is the first different calendar field in above order. For example: the largest different calendar fields between "Jan 10, 2007" and "Feb 20, 2008" is year.
For other calendar fields, the compact interval formatting is not supported. And the interval format will be fall back to fall-back patterns, which is mostly "{date0} - {date1}".
There is a set of pre-defined static skeleton strings in DateFormat, There are pre-defined interval patterns for those pre-defined skeletons in locales' resource files. For example, for a skeleton YEAR_ABBR_MONTH_DAY, which is "yMMMd", in en_US, if the largest different calendar field between date1 and date2 is "year", the date interval pattern is "MMM d, yyyy - MMM d, yyyy", such as "Jan 10, 2007 - Jan 10, 2008". If the largest different calendar field between date1 and date2 is "month", the date interval pattern is "MMM d - MMM d, yyyy", such as "Jan 10 - Feb 10, 2007". If the largest different calendar field between date1 and date2 is "day", the date interval pattern is ""MMM d-d, yyyy", such as "Jan 10-20, 2007". For date skeleton, the interval patterns when year, or month, or date is different are defined in resource files. For time skeleton, the interval patterns when am/pm, or hour, or minute is different are defined in resource files.
If a skeleton is not found in a locale's DateIntervalInfo, which means the interval patterns for the skeleton is not defined in resource file, the interval pattern will falls back to the interval "fallback" pattern defined in resource file. If the interval "fallback" pattern is not defined, the default fall-back is "{date0} - {data1}".
For the combination of date and time, The rule to genearte interval patterns are:
If two dates are the same, the interval pattern is the single date pattern. For example, interval pattern from "Jan 10, 2007" to "Jan 10, 2007" is "Jan 10, 2007". Or if the presenting fields between 2 dates have the exact same values, the interval pattern is the single date pattern. For example, if user only requests year and month, the interval pattern from "Jan 10, 2007" to "Jan 20, 2007" is "Jan 2007".
DateIntervalFormat needs the following information for correct formatting: time zone, calendar type, pattern, date format symbols, and date interval patterns. It can be instantiated in several ways:
For the calendar field pattern letter, such as G, y, M, d, a, h, H, m, s etc. DateIntervalFormat uses the same syntax as that of DateTime format.
Code Sample: general usage
// the date interval object which the DateIntervalFormat formats on
// and parses into
DateInterval dtInterval = new DateInterval(1000*3600*24L, 1000*3600*24*2L);
DateIntervalFormat dtIntervalFmt = DateIntervalFormat.getInstance(
DateFormat.YEAR_MONTH_DAY, new Locale("en", "GB", ""));
StringBuffer result = new StringBuffer("");
FieldPosition pos = new FieldPosition(-1);
// formatting
dtIntervalFmt.format(dtInterval, result, pos);
assertEquals("interval", "1–2 January 1970", result.toString());
Code Sample: for powerful users who wants to use their own interval pattern
import com.ibm.icu.text.DateIntervalInfo;
import com.ibm.icu.text.DateIntervalFormat;
....................
// Get DateIntervalFormat instance using default locale
DateIntervalFormat dtitvfmt = DateIntervalFormat.getInstance(YEAR_MONTH_DAY);
// Create an empty DateIntervalInfo object, which does not have any interval patterns inside.
dtitvinf = new DateIntervalInfo();
// a series of set interval patterns.
// Only ERA, YEAR, MONTH, DATE, DAY_OF_MONTH, DAY_OF_WEEK, AM_PM, HOUR, HOUR_OF_DAY,
MINUTE, SECOND and MILLISECOND are supported.
dtitvinf.setIntervalPattern("yMMMd", Calendar.YEAR, "'y ~ y'");
dtitvinf.setIntervalPattern("yMMMd", Calendar.MONTH, "yyyy 'diff' MMM d - MMM d");
dtitvinf.setIntervalPattern("yMMMd", Calendar.DATE, "yyyy MMM d ~ d");
dtitvinf.setIntervalPattern("yMMMd", Calendar.HOUR_OF_DAY, "yyyy MMM d HH:mm ~ HH:mm");
// Set fallback interval pattern. Fallback pattern is used when interval pattern is not found.
// If the fall-back pattern is not set, falls back to {date0} - {date1} if interval pattern is not found.
dtitvinf.setFallbackIntervalPattern("{0} - {1}");
// Set above DateIntervalInfo object as the interval patterns of date interval formatter
dtitvfmt.setDateIntervalInfo(dtitvinf);
// Prepare to format
pos = new FieldPosition(0);
str = new StringBuffer("");
// The 2 calendars should be equivalent, otherwise, IllegalArgumentException will be thrown by format()
Calendar fromCalendar = (Calendar) dtfmt.getCalendar().clone();
Calendar toCalendar = (Calendar) dtfmt.getCalendar().clone();
fromCalendar.setTimeInMillis(....);
toCalendar.setTimeInMillis(...);
//Formatting given 2 calendars
dtitvfmt.format(fromCalendar, toCalendar, str, pos);
| Modifier and Type | Class and Description |
|---|---|
| pack-priv static class | DateIntervalFormat.BestMatchInfo
Used to save the information for a skeleton's best match skeleton. |
| private static class | DateIntervalFormat.FormatOutput
Used to output information during formatting. |
| public static class | DateIntervalFormat.FormattedDateInterval
An immutable class containing the result of a date interval formatting operation. |
| private static class | |
| public static class | DateIntervalFormat.SpanField
Class for span fields in FormattedDateInterval. |
| Modifier and Type | Field and Description |
|---|---|
| private DisplayContext | |
| private SimpleDateFormat | |
| private String | |
| private String | |
| private Calendar | |
| private DateIntervalInfo | |
| private transient Map | fIntervalPatterns
Interval patterns for this instance's locale. |
| private String | |
| private String | |
| private Calendar | |
| private boolean | |
| private static ICUCache | |
| private static final long |
| Access | Constructor and Description |
|---|---|
| private | |
| public | DateIntervalFormat(String
the skeleton of the date formatter skeleton, DateIntervalInfo the DateIntervalInfo object to be adopted. dtItvInfo, SimpleDateFormat will be used for formatting simpleDateFormat)
Deprecated
This API is ICU internal only.
Construct a DateIntervalFormat from DateFormat,
a DateIntervalInfo, and skeleton.
|
| private |
| Modifier and Type | Method and Description |
|---|---|
| private static String | adjustFieldWidth(String inputSkeleton, String bestMatchSkeleton, String bestMatchIntervalPattern, int differenceInfo, boolean suppressDayPeriodField)
|
| public synchronized Object | Returns: A copy of the object.Overrides java. |
| private void | concatSingleDate2TimeInterval(String dtfmt, String datePattern, int field, Map<String, DateIntervalInfo.
|
| private final StringBuffer | fallbackFormat(Calendar fromCalendar, Calendar toCalendar, boolean fromToOnSameDay, StringBuffer appendTo, FieldPosition pos, DateIntervalFormat.
|
| private final StringBuffer | fallbackFormat(Calendar fromCalendar, Calendar toCalendar, boolean fromToOnSameDay, StringBuffer appendTo, FieldPosition pos, DateIntervalFormat.
|
| private final void | fallbackFormatRange(Calendar fromCalendar, Calendar toCalendar, StringBuffer appendTo, StringBuilder patternSB, FieldPosition pos, DateIntervalFormat.
Like fallbackFormat, but specifically for ranges. |
| private static boolean | |
| private static String | findReplaceInPattern(String
The string to perform the find-replace operation on. targetString, String The string to search for and replace in the target string. strToReplace, String The string to substitute in wherever strToReplaceWith)stringToReplace was found.Does the same thing as String.replace(), except that it won't perform the substitution inside quoted literal text. |
| public final StringBuffer | Returns: Reference to 'appendTo' parameter.The object to format.
Must be a DateInterval. obj, StringBuffer Output parameter to receive result.
Result is appended to existing contents. appendTo, FieldPosition On input: an alignment field, if desired.
On output: the offsets of the alignment field.
There may be multiple instances of a given field type
in an interval format; in this case the fieldPosition
offsets refer to the first instance. fieldPosition)Implements abstract java. |
| public final StringBuffer | Returns: Reference to 'appendTo' parameter.DateInterval to be formatted. dtInterval, StringBuffer Output parameter to receive result.
Result is appended to existing contents. appendTo, FieldPosition On input: an alignment field, if desired.
On output: the offsets of the alignment field.
There may be multiple instances of a given field type
in an interval format; in this case the fieldPosition
offsets refer to the first instance. fieldPosition)Format a DateInterval to produce a string. |
| public final StringBuffer | Returns: Reference to 'appendTo' parameter.calendar set to the from date in date interval
to be formatted into date interval string fromCalendar, Calendar calendar set to the to date in date interval
to be formatted into date interval string toCalendar, StringBuffer Output parameter to receive result.
Result is appended to existing contents. appendTo, FieldPosition On input: an alignment field, if desired.
On output: the offsets of the alignment field.
There may be multiple instances of a given field type
in an interval format; in this case the fieldPosition
offsets refer to the first instance. pos)Format 2 Calendars to produce a string. |
| private synchronized StringBuffer | formatImpl(Calendar fromCalendar, Calendar toCalendar, StringBuffer appendTo, FieldPosition pos, DateIntervalFormat.
|
| private synchronized StringBuffer | formatIntervalImpl(DateInterval dtInterval, StringBuffer appendTo, FieldPosition pos, DateIntervalFormat.
|
| public DateIntervalFormat. | Returns: A FormattedDateInterval containing the format result.DateInterval to be formatted. dtInterval)Format a DateInterval to produce a FormattedDateInterval. |
| public DateIntervalFormat. | Returns: A FormattedDateInterval containing the format result.calendar set to the from date in date interval
to be formatted into date interval string fromCalendar, Calendar calendar set to the to date in date interval
to be formatted into date interval string toCalendar)Format 2 Calendars to produce a FormattedDateInterval. |
| private void | genFallbackPattern(int field, String skeleton, Map<String, DateIntervalInfo.
|
| private DateIntervalFormat. | genIntervalPattern(int field, String skeleton, String bestSkeleton, int differenceInfo, Map<String, DateIntervalInfo.
|
| private boolean | genSeparateDateTimePtn(String dateSkeleton, String timeSkeleton, Map<String, DateIntervalInfo.
|
| private String | Returns: Concatenation DateTime pattern.Locale to retrieve. locale)Retrieves the concatenation DateTime pattern from the resource bundle. |
| public DisplayContext | Returns: the current DisplayContext setting for the specified typethe DisplayContext.Type whose value to return typeICU |
| public synchronized DateFormat | Returns: a copy of the date formatter associated with this date interval formatter.Gets the date formatter |
| public DateIntervalInfo | Returns: a copy of the date time interval patterns associated with this date interval formatter.Gets the date time interval patterns. |
| private static void | getDateTimeSkeleton(String skeleton, StringBuilder dateSkeleton, StringBuilder normalizedDateSkeleton, StringBuilder timeSkeleton, StringBuilder normalizedTimeSkeleton)
|
| public static final DateIntervalFormat | Returns: a date time interval formatter.the skeleton on which interval format based. skeleton)Construct a DateIntervalFormat from skeleton and the default |
| public static final DateIntervalFormat | Returns: a date time interval formatter.the skeleton on which interval format based. skeleton, Locale the given locale locale)Construct a DateIntervalFormat from skeleton and a given locale. |
| public static final DateIntervalFormat | Returns: a date time interval formatter.the skeleton on which interval format based. skeleton, ULocale the given locale locale)Construct a DateIntervalFormat from skeleton and a given locale. |
| public static final DateIntervalFormat | Returns: a date time interval formatter.the skeleton on which interval format based. skeleton, DateIntervalInfo the DateIntervalInfo object to be adopted. dtitvinf)Construct a DateIntervalFormat from skeleton
DateIntervalInfo, and the default |
| public static final DateIntervalFormat | Returns: a date time interval formatter.the skeleton on which interval format based. skeleton, Locale the given locale locale, DateIntervalInfo the DateIntervalInfo object to be adopted. dtitvinf)Construct a DateIntervalFormat from skeleton a DateIntervalInfo, and the given locale. |
| public static final DateIntervalFormat | Returns: a date time interval formatter.the skeleton on which interval format based. skeleton, ULocale the given locale locale, DateIntervalInfo the DateIntervalInfo object to be adopted. dtitvinf)Construct a DateIntervalFormat from skeleton a DateIntervalInfo, and the given locale. |
| public String | getPatterns(Calendar fromCalendar, Calendar toCalendar, Output<String> part2)
Deprecated
This API is ICU internal only.
|
| public Map | getRawPatterns()
Deprecated
This API is ICU internal only.
Get the internal patterns for the skeleton
|
| public TimeZone | Returns: A copy of the TimeZone associated with this date interval formatter.Get the TimeZone |
| private Map | |
| private void | |
| private String | |
| public Object | Returns: A newly createdFormattable object, or NULL
on failure.The string to be parsed into an object. source, ParsePosition The position to start parsing at. Since no parsing
is supported, upon return this param is unchanged. parse_pos)Implements abstract java.
Deprecated
This API is ICU internal only.
Date interval parsing is not supported.
|
| private void | |
| public void | |
| public void | setDateIntervalInfo(DateIntervalInfo
the given interval patterns to copy. newItvPattern)Set the date time interval patterns. |
| public void | setTimeZone(TimeZone
The new TimeZone, will be cloned for use by this DateIntervalFormat. zone)Set the TimeZone for the calendar used by this DateIntervalFormat object. |