public class DateIntervalFormat extends UFormat
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 |
---|---|
static class |
DateIntervalFormat.FormattedDateInterval
An immutable class containing the result of a date interval formatting operation.
|
static class |
DateIntervalFormat.SpanField
Class for span fields in FormattedDateInterval.
|
Format.Field
Constructor and Description |
---|
DateIntervalFormat(String skeleton,
DateIntervalInfo dtItvInfo,
SimpleDateFormat simpleDateFormat)
Deprecated.
This API is ICU internal only.
|
Modifier and Type | Method and Description |
---|---|
Object |
clone()
Clone this Format object polymorphically.
|
StringBuffer |
format(Calendar fromCalendar,
Calendar toCalendar,
StringBuffer appendTo,
FieldPosition pos)
Format 2 Calendars to produce a string.
|
StringBuffer |
format(DateInterval dtInterval,
StringBuffer appendTo,
FieldPosition fieldPosition)
Format a DateInterval to produce a string.
|
StringBuffer |
format(Object obj,
StringBuffer appendTo,
FieldPosition fieldPosition)
Format an object to produce a string.
|
StringBuffer |
format(Temporal fromTemporal,
Temporal toTemporal,
StringBuffer appendTo,
FieldPosition pos)
Format two
Temporal s to produce a string. |
DateIntervalFormat.FormattedDateInterval |
formatToValue(Calendar fromCalendar,
Calendar toCalendar)
Format 2 Calendars to produce a FormattedDateInterval.
|
DateIntervalFormat.FormattedDateInterval |
formatToValue(DateInterval dtInterval)
Format a DateInterval to produce a FormattedDateInterval.
|
DateIntervalFormat.FormattedDateInterval |
formatToValue(Temporal fromTemporal,
Temporal toTemporal)
Format two
Temporal s to produce a FormattedDateInterval. |
DisplayContext |
getContext(DisplayContext.Type type)
[icu] Get the formatter's DisplayContext value for the specified DisplayContext.Type,
such as CAPITALIZATION.
|
DateFormat |
getDateFormat()
Gets the date formatter
|
DateIntervalInfo |
getDateIntervalInfo()
Gets the date time interval patterns.
|
static DateIntervalFormat |
getInstance(String skeleton)
Construct a DateIntervalFormat from skeleton and the default
FORMAT locale. |
static DateIntervalFormat |
getInstance(String skeleton,
DateIntervalInfo dtitvinf)
Construct a DateIntervalFormat from skeleton
DateIntervalInfo, and the default
FORMAT locale. |
static DateIntervalFormat |
getInstance(String skeleton,
Locale locale)
Construct a DateIntervalFormat from skeleton and a given locale.
|
static DateIntervalFormat |
getInstance(String skeleton,
Locale locale,
DateIntervalInfo dtitvinf)
Construct a DateIntervalFormat from skeleton
a DateIntervalInfo, and the given locale.
|
static DateIntervalFormat |
getInstance(String skeleton,
ULocale locale)
Construct a DateIntervalFormat from skeleton and a given locale.
|
static DateIntervalFormat |
getInstance(String skeleton,
ULocale locale,
DateIntervalInfo dtitvinf)
Construct a DateIntervalFormat from skeleton
a DateIntervalInfo, and the given locale.
|
String |
getPatterns(Calendar fromCalendar,
Calendar toCalendar,
Output<String> part2)
Deprecated.
This API is ICU internal only.
|
Map<String,DateIntervalInfo.PatternInfo> |
getRawPatterns()
Deprecated.
This API is ICU internal only.
|
TimeZone |
getTimeZone()
Get the TimeZone
|
Object |
parseObject(String source,
ParsePosition parse_pos)
Deprecated.
This API is ICU internal only.
|
void |
setContext(DisplayContext context)
[icu] Set a particular DisplayContext value in the formatter,
such as CAPITALIZATION_FOR_STANDALONE.
|
void |
setDateIntervalInfo(DateIntervalInfo newItvPattern)
Set the date time interval patterns.
|
void |
setTimeZone(TimeZone zone)
Set the TimeZone for the calendar used by this DateIntervalFormat object.
|
format, formatToCharacterIterator, parseObject
@Deprecated public DateIntervalFormat(String skeleton, DateIntervalInfo dtItvInfo, SimpleDateFormat simpleDateFormat)
skeleton
- the skeleton of the date formatterdtItvInfo
- the DateIntervalInfo object to be adopted.simpleDateFormat
- will be used for formattingpublic static final DateIntervalFormat getInstance(String skeleton)
FORMAT
locale.
This is a convenient override of
getInstance(String skeleton, ULocale locale)
with the value of locale as default FORMAT
locale.skeleton
- the skeleton on which interval format based.ULocale.Category.FORMAT
public static final DateIntervalFormat getInstance(String skeleton, Locale locale)
Example code:
import java.util.Date; import com.ibm.icu.text.DateFormat; import com.ibm.icu.text.DateIntervalFormat; import com.ibm.icu.text.DateIntervalInfo; import com.ibm.icu.util.Calendar; import com.ibm.icu.util.DateInterval; import com.ibm.icu.util.GregorianCalendar; import com.ibm.icu.util.ULocale; final Date date[] = { new GregorianCalendar(2007,10,10,10,10,10).getTime(), new GregorianCalendar(2008,10,10,10,10,10).getTime(), new GregorianCalendar(2008,11,10,10,10,10).getTime(), new GregorianCalendar(2008,11,10,15,10,10).getTime(), }; final DateInterval dtitv[] = { new DateInterval(date[0].getTime(),date[1].getTime()), new DateInterval(date[1].getTime(),date[2].getTime()), new DateInterval(date[2].getTime(),date[3].getTime()), }; final String [] skeletons = { DateFormat.YEAR_ABBR_MONTH_DAY, DateFormat.MONTH_DAY, DateFormat.HOUR_MINUTE, }; System.out.printf("%-15s%-35s%-35s%-35s%-35s\n", "Skeleton", "from","to","Date Interval in en_US", "Date Interval in Ja"); int i=0; for (String skeleton:skeletons) { System.out.printf("%-15s%-35s%-35s", skeleton,date[i].toString(), date[i+1].toString()); DateIntervalFormat dtitvfmtEn = DateIntervalFormat.getInstance(skeleton, ULocale.ENGLISH); DateIntervalFormat dtitvfmtJa = DateIntervalFormat.getInstance(skeleton, ULocale.JAPANESE); System.out.printf("%-35s%-35s\n", dtitvfmtEn.format(dtitv[i]),dtitvfmtJa.format(dtitv[i])); i++; } /** output of the sample code: ********************************************************************************************************************************************************* Skeleton from to Date Interval in en_US Date Interval in Ja yMMMd Sat Nov 10 10:10:10 EST 2007 Mon Nov 10 10:10:10 EST 2008 Nov 10, 2007 – Nov 10, 2008 2007年11月10日~2008年11月10日 MMMMd Mon Nov 10 10:10:10 EST 2008 Wed Dec 10 10:10:10 EST 2008 November 10 – December 10 11月10日~12月10日 jm Wed Dec 10 10:10:10 EST 2008 Wed Dec 10 15:10:10 EST 2008 10:10 AM – 3:10 PM 10:10~15:10 *********************************************************************************************************************************************************/
skeleton
- the skeleton on which interval format based.locale
- the given localepublic static final DateIntervalFormat getInstance(String skeleton, ULocale locale)
In this factory method, the date interval pattern information is load from resource files. Users are encouraged to created date interval formatter this way and to use the pre-defined skeleton macros.
There are pre-defined skeletons in DateFormat, such as MONTH_DAY, YEAR_MONTH_WEEKDAY_DAY etc. Those skeletons have pre-defined interval patterns in resource files. Users are encouraged to use them. For example: DateIntervalFormat.getInstance(DateFormat.MONTH_DAY, false, loc); The given Locale provides the interval patterns. For example, for en_GB, if skeleton is YEAR_ABBR_MONTH_WEEKDAY_DAY, which is "yMMMEEEd", the interval patterns defined in resource file to above skeleton are: "EEE, d MMM, yyyy - EEE, d MMM, yyyy" for year differs, "EEE, d MMM - EEE, d MMM, yyyy" for month differs, "EEE, d - EEE, d MMM, yyyy" for day differs,
skeleton
- the skeleton on which interval format based.locale
- the given localepublic static final DateIntervalFormat getInstance(String skeleton, DateIntervalInfo dtitvinf)
FORMAT
locale.
This is a convenient override of
getInstance(String skeleton, ULocale locale, DateIntervalInfo dtitvinf)
with the locale value as default FORMAT
locale.skeleton
- the skeleton on which interval format based.dtitvinf
- the DateIntervalInfo object to be adopted.ULocale.Category.FORMAT
public static final DateIntervalFormat getInstance(String skeleton, Locale locale, DateIntervalInfo dtitvinf)
Example code:
final Date date[] = { new GregorianCalendar(2007,9,10,10,10,10).getTime(), new GregorianCalendar(2007,10,10,10,10,10).getTime(), new GregorianCalendar(2007,10,10,22,10,10).getTime(), }; final DateInterval dtitv[] = { new DateInterval(date[0].getTime(),date[1].getTime()), new DateInterval(date[1].getTime(),date[2].getTime()), }; final String [] skeletons = { DateFormat.YEAR_ABBR_MONTH_DAY, DateFormat.HOUR24_MINUTE, }; System.out.printf("%-15s%-35s%-35s%-45s%-35s\n", "Skeleton", "from","to", "Date Interval in en_US", "Date Interval in Ja"); // Create an empty DateIntervalInfo object DateIntervalInfo dtitvinf = new DateIntervalInfo(ULocale.ENGLISH); // Set Date Time internal pattern for MONTH, DAY_OF_MONTH, HOUR_OF_DAY dtitvinf.setIntervalPattern("yMMMd", Calendar.MONTH, "y 'Diff' MMM d --- MMM d"); dtitvinf.setIntervalPattern("Hm", Calendar.HOUR_OF_DAY, "yyyy MMM d HH:mm ~ HH:mm"); // Set fallback interval pattern dtitvinf.setFallbackIntervalPattern("{0} ~~~ {1}"); // Get the DateIntervalFormat with the custom pattern for (String skeleton:skeletons){ for (int i=0;i<2;i++) { System.out.printf("%-15s%-35s%-35s", skeleton,date[i].toString(), date[i+1].toString()); DateIntervalFormat dtitvfmtEn = DateIntervalFormat.getInstance(skeleton,ULocale.ENGLISH,dtitvinf); DateIntervalFormat dtitvfmtJa = DateIntervalFormat.getInstance(skeleton,ULocale.JAPANESE,dtitvinf); System.out.printf("%-45s%-35s\n", dtitvfmtEn.format(dtitv[i]),dtitvfmtJa.format(dtitv[i])); } } /** output of the sample code: ************************************************************************************************************************************************************************* Skeleton from to Date Interval in en_US Date Interval in Ja yMMMd Wed Oct 10 10:10:10 EDT 2007 Sat Nov 10 10:10:10 EST 2007 2007 Diff Oct 10 --- Nov 10 2007 Diff 10月 10 --- 11月 10 yMMMd Sat Nov 10 10:10:10 EST 2007 Sat Nov 10 22:10:10 EST 2007 Nov 10, 2007 2007年11月10日 Hm Wed Oct 10 10:10:10 EDT 2007 Sat Nov 10 10:10:10 EST 2007 10/10/2007, 10:10 ~~~ 11/10/2007, 10:10 2007/10/10 10:10 ~~~ 2007/11/10 10:10 Hm Sat Nov 10 10:10:10 EST 2007 Sat Nov 10 22:10:10 EST 2007 2007 Nov 10 10:10 ~ 22:10 2007 11月 10 10:10 ~ 22:10 *************************************************************************************************************************************************************************/
skeleton
- the skeleton on which interval format based.locale
- the given localedtitvinf
- the DateIntervalInfo object to be adopted.public static final DateIntervalFormat getInstance(String skeleton, ULocale locale, DateIntervalInfo dtitvinf)
In this factory method, user provides its own date interval pattern information, instead of using those pre-defined data in resource file. This factory method is for powerful users who want to provide their own interval patterns.
There are pre-defined skeleton in DateFormat, such as MONTH_DAY, YEAR_MONTH_WEEKDAY_DAY etc. Those skeletons have pre-defined interval patterns in resource files. Users are encouraged to use them. For example: DateIntervalFormat.getInstance(DateFormat.MONTH_DAY, false, loc,itvinf); the DateIntervalInfo provides the interval patterns. User are encouraged to set default interval pattern in DateIntervalInfo as well, if they want to set other interval patterns ( instead of reading the interval patterns from resource files). When the corresponding interval pattern for a largest calendar different field is not found ( if user not set it ), interval format fallback to the default interval pattern. If user does not provide default interval pattern, it fallback to "{date0} - {date1}"
skeleton
- the skeleton on which interval format based.locale
- the given localedtitvinf
- the DateIntervalInfo object to be adopted.public Object clone()
public final StringBuffer format(Object obj, StringBuffer appendTo, FieldPosition fieldPosition)
format
in class Format
obj
- The object to format.
Must be a DateInterval.appendTo
- Output parameter to receive result.
Result is appended to existing contents.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.IllegalArgumentException
- if the formatted object is not
DateInterval objectpublic final StringBuffer format(DateInterval dtInterval, StringBuffer appendTo, FieldPosition fieldPosition)
dtInterval
- DateInterval to be formatted.appendTo
- Output parameter to receive result.
Result is appended to existing contents.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.public DateIntervalFormat.FormattedDateInterval formatToValue(DateInterval dtInterval)
dtInterval
- DateInterval to be formatted.@Deprecated public String getPatterns(Calendar fromCalendar, Calendar toCalendar, Output<String> part2)
public final StringBuffer format(Calendar fromCalendar, Calendar toCalendar, StringBuffer appendTo, FieldPosition pos)
fromCalendar
- calendar set to the from date in date interval
to be formatted into date interval stringtoCalendar
- calendar set to the to date in date interval
to be formatted into date interval stringappendTo
- Output parameter to receive result.
Result is appended to existing contents.pos
- 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.IllegalArgumentException
- if the two calendars are not equivalent.public final StringBuffer format(Temporal fromTemporal, Temporal toTemporal, StringBuffer appendTo, FieldPosition pos)
Temporal
s to produce a string.fromTemporal
- temporal set to the start of the interval
to be formatted into a stringtoTemporal
- temporal set to the end of the interval
to be formatted into a stringappendTo
- Output parameter to receive result.
Result is appended to existing contents.pos
- 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.IllegalArgumentException
- if the two calendars are not equivalent.public DateIntervalFormat.FormattedDateInterval formatToValue(Calendar fromCalendar, Calendar toCalendar)
fromCalendar
- calendar set to the from date in date interval
to be formatted into date interval stringtoCalendar
- calendar set to the to date in date interval
to be formatted into date interval stringpublic DateIntervalFormat.FormattedDateInterval formatToValue(Temporal fromTemporal, Temporal toTemporal)
Temporal
s to produce a FormattedDateInterval.
The FormattedDateInterval exposes field information about the formatted string.fromTemporal
- temporal set to the start of the interval
to be formatted into a stringtoTemporal
- temporal set to the end of the interval
to be formatted into a string@Deprecated public Object parseObject(String source, ParsePosition parse_pos)
This method should handle parsing of date time interval strings into Formattable objects with DateInterval type, which is a pair of UDate.
Before calling, set parse_pos.index to the offset you want to start parsing at in the source. After calling, parse_pos.index is the end of the text you parsed. If error occurs, index is unchanged.
When parsing, leading whitespace is discarded (with a successful parse), while trailing whitespace is left as is.
See Format.parseObject() for more.
parseObject
in class Format
source
- The string to be parsed into an object.parse_pos
- The position to start parsing at. Since no parsing
is supported, upon return this param is unchanged.Formattable
object, or NULL
on failure.public DateIntervalInfo getDateIntervalInfo()
public void setDateIntervalInfo(DateIntervalInfo newItvPattern)
newItvPattern
- the given interval patterns to copy.public TimeZone getTimeZone()
public void setTimeZone(TimeZone zone)
zone
- The new TimeZone, will be cloned for use by this DateIntervalFormat.public void setContext(DisplayContext context)
context
- The DisplayContext value to set.public DisplayContext getContext(DisplayContext.Type type)
type
- the DisplayContext.Type whose value to returnpublic DateFormat getDateFormat()
@Deprecated public Map<String,DateIntervalInfo.PatternInfo> getRawPatterns()
Copyright © 2016 Unicode, Inc. and others.