Class DateIntervalFormat
- java.lang.Object
-
- java.text.Format
-
- com.ibm.icu.text.UFormat
-
- com.ibm.icu.text.DateIntervalFormat
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Cloneable
public class DateIntervalFormat extends UFormat
DateIntervalFormat is a class for formatting and parsing date intervals in a language-independent manner. Only formatting is supported. Parsing is not supported.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
- only keeps the field pattern letter and ignores all other parts in a pattern, such as space, punctuations, and string literals.
- hides the order of fields.
- might hide a field's pattern letter length. For those non-digit calendar fields, the pattern letter length is important, such as MMM, MMMM, and MMMMM; EEE and EEEE, and the field's pattern letter length is honored. For the digit calendar fields, such as M or MM, d or dd, yy or yyyy, the field pattern length is ignored and the best match, which is defined in date time patterns, will be returned without honor the field pattern letter length in 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:
- when the year, month, or day differs, falls back to fall-back interval pattern, which mostly is the concatenate the two original expressions with a separator between, For example, interval pattern from "Jan 10, 2007 10:10 am" to "Jan 11, 2007 10:10am" is "Jan 10, 2007 10:10 am - Jan 11, 2007 10:10am"
- otherwise, present the date followed by the range expression for the time. For example, interval pattern from "Jan 10, 2007 10:10 am" to "Jan 10, 2007 11:10am" is "Jan 10, 2007 10:10 am - 11:10am"
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:
- create an instance using default or given locale plus given skeleton. Users are encouraged to created date interval formatter this way and to use the pre-defined skeleton macros, such as YEAR_NUM_MONTH, which consists the calendar fields and the format style.
- create an instance using default or given locale plus given skeleton plus a given DateIntervalInfo. This factory method is for powerful users who want to provide their own interval patterns. Locale provides the timezone, calendar, and format symbols information. Local plus skeleton provides full pattern information. DateIntervalInfo provides the date interval patterns.
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);
Synchronization
The format methods of DateIntervalFormat may be used concurrently from multiple threads. Functions that alter the state of a DateIntervalFormat object (setters) may not be used concurrently with any other functions.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) 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.static class
DateIntervalFormat.FormattedDateInterval
An immutable class containing the result of a date interval formatting operation.private static class
DateIntervalFormat.SkeletonAndItsBestMatch
static class
DateIntervalFormat.SpanField
Class for span fields in FormattedDateInterval.
-
Field Summary
Fields Modifier and Type Field Description private DisplayContext
fCapitalizationSetting
private SimpleDateFormat
fDateFormat
private java.lang.String
fDatePattern
private java.lang.String
fDateTimeFormat
private Calendar
fFromCalendar
private DateIntervalInfo
fInfo
private java.util.Map<java.lang.String,DateIntervalInfo.PatternInfo>
fIntervalPatterns
Interval patterns for this instance's locale.private java.lang.String
fSkeleton
private java.lang.String
fTimePattern
private Calendar
fToCalendar
private boolean
isDateIntervalInfoDefault
private static ICUCache<java.lang.String,java.util.Map<java.lang.String,DateIntervalInfo.PatternInfo>>
LOCAL_PATTERN_CACHE
private static long
serialVersionUID
-
Constructor Summary
Constructors Modifier Constructor Description private
DateIntervalFormat()
DateIntervalFormat(java.lang.String skeleton, DateIntervalInfo dtItvInfo, SimpleDateFormat simpleDateFormat)
Deprecated.This API is ICU internal only.private
DateIntervalFormat(java.lang.String skeleton, ULocale locale, SimpleDateFormat simpleDateFormat)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description private static java.lang.String
adjustFieldWidth(java.lang.String inputSkeleton, java.lang.String bestMatchSkeleton, java.lang.String bestMatchIntervalPattern, int differenceInfo, boolean suppressDayPeriodField)
java.lang.Object
clone()
Clone this Format object polymorphically.private void
concatSingleDate2TimeInterval(java.lang.String dtfmt, java.lang.String datePattern, int field, java.util.Map<java.lang.String,DateIntervalInfo.PatternInfo> intervalPatterns)
private java.lang.StringBuffer
fallbackFormat(Calendar fromCalendar, Calendar toCalendar, boolean fromToOnSameDay, java.lang.StringBuffer appendTo, java.text.FieldPosition pos, DateIntervalFormat.FormatOutput output, java.util.List<java.text.FieldPosition> attributes)
private java.lang.StringBuffer
fallbackFormat(Calendar fromCalendar, Calendar toCalendar, boolean fromToOnSameDay, java.lang.StringBuffer appendTo, java.text.FieldPosition pos, DateIntervalFormat.FormatOutput output, java.util.List<java.text.FieldPosition> attributes, java.lang.String fullPattern)
private void
fallbackFormatRange(Calendar fromCalendar, Calendar toCalendar, java.lang.StringBuffer appendTo, java.lang.StringBuilder patternSB, java.text.FieldPosition pos, DateIntervalFormat.FormatOutput output, java.util.List<java.text.FieldPosition> attributes)
Like fallbackFormat, but specifically for ranges.private static boolean
fieldExistsInSkeleton(int field, java.lang.String skeleton)
private static java.lang.String
findReplaceInPattern(java.lang.String targetString, java.lang.String strToReplace, java.lang.String strToReplaceWith)
Does the same thing as String.replace(), except that it won't perform the substitution inside quoted literal text.java.lang.StringBuffer
format(Calendar fromCalendar, Calendar toCalendar, java.lang.StringBuffer appendTo, java.text.FieldPosition pos)
Format 2 Calendars to produce a string.java.lang.StringBuffer
format(DateInterval dtInterval, java.lang.StringBuffer appendTo, java.text.FieldPosition fieldPosition)
Format a DateInterval to produce a string.java.lang.StringBuffer
format(java.lang.Object obj, java.lang.StringBuffer appendTo, java.text.FieldPosition fieldPosition)
Format an object to produce a string.java.lang.StringBuffer
format(java.time.temporal.Temporal fromTemporal, java.time.temporal.Temporal toTemporal, java.lang.StringBuffer appendTo, java.text.FieldPosition pos)
Format twoTemporal
s to produce a string.private java.lang.StringBuffer
formatImpl(Calendar fromCalendar, Calendar toCalendar, java.lang.StringBuffer appendTo, java.text.FieldPosition pos, DateIntervalFormat.FormatOutput output, java.util.List<java.text.FieldPosition> attributes)
private java.lang.StringBuffer
formatIntervalImpl(DateInterval dtInterval, java.lang.StringBuffer appendTo, java.text.FieldPosition pos, DateIntervalFormat.FormatOutput output, java.util.List<java.text.FieldPosition> attributes)
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(java.time.temporal.Temporal fromTemporal, java.time.temporal.Temporal toTemporal)
Format twoTemporal
s to produce a FormattedDateInterval.private void
genFallbackPattern(int field, java.lang.String skeleton, java.util.Map<java.lang.String,DateIntervalInfo.PatternInfo> intervalPatterns, DateTimePatternGenerator dtpng)
private DateIntervalFormat.SkeletonAndItsBestMatch
genIntervalPattern(int field, java.lang.String skeleton, java.lang.String bestSkeleton, int differenceInfo, java.util.Map<java.lang.String,DateIntervalInfo.PatternInfo> intervalPatterns)
private boolean
genSeparateDateTimePtn(java.lang.String dateSkeleton, java.lang.String timeSkeleton, java.util.Map<java.lang.String,DateIntervalInfo.PatternInfo> intervalPatterns, DateTimePatternGenerator dtpng)
private java.lang.String
getConcatenationPattern(ULocale locale)
Retrieves the concatenation DateTime pattern from the resource bundle.DisplayContext
getContext(DisplayContext.Type type)
Get the formatter's DisplayContext value for the specified DisplayContext.Type, such as CAPITALIZATION.DateFormat
getDateFormat()
Gets the date formatterDateIntervalInfo
getDateIntervalInfo()
Gets the date time interval patterns.private static void
getDateTimeSkeleton(java.lang.String skeleton, java.lang.StringBuilder dateSkeleton, java.lang.StringBuilder normalizedDateSkeleton, java.lang.StringBuilder timeSkeleton, java.lang.StringBuilder normalizedTimeSkeleton)
static DateIntervalFormat
getInstance(java.lang.String skeleton)
Construct a DateIntervalFormat from skeleton and the defaultFORMAT
locale.static DateIntervalFormat
getInstance(java.lang.String skeleton, DateIntervalInfo dtitvinf)
Construct a DateIntervalFormat from skeleton DateIntervalInfo, and the defaultFORMAT
locale.static DateIntervalFormat
getInstance(java.lang.String skeleton, ULocale locale)
Construct a DateIntervalFormat from skeleton and a given locale.static DateIntervalFormat
getInstance(java.lang.String skeleton, ULocale locale, DateIntervalInfo dtitvinf)
Construct a DateIntervalFormat from skeleton a DateIntervalInfo, and the given locale.static DateIntervalFormat
getInstance(java.lang.String skeleton, java.util.Locale locale)
Construct a DateIntervalFormat from skeleton and a given locale.static DateIntervalFormat
getInstance(java.lang.String skeleton, java.util.Locale locale, DateIntervalInfo dtitvinf)
Construct a DateIntervalFormat from skeleton a DateIntervalInfo, and the given locale.java.lang.String
getPatterns(Calendar fromCalendar, Calendar toCalendar, Output<java.lang.String> part2)
Deprecated.This API is ICU internal only.java.util.Map<java.lang.String,DateIntervalInfo.PatternInfo>
getRawPatterns()
Deprecated.This API is ICU internal only.TimeZone
getTimeZone()
Get the TimeZoneprivate java.util.Map<java.lang.String,DateIntervalInfo.PatternInfo>
initializeIntervalPattern(java.lang.String fullPattern, ULocale locale)
private void
initializePattern(ICUCache<java.lang.String,java.util.Map<java.lang.String,DateIntervalInfo.PatternInfo>> cache)
private java.lang.String
normalizeHourMetacharacters(java.lang.String skeleton, ULocale locale)
java.lang.Object
parseObject(java.lang.String source, java.text.ParsePosition parse_pos)
Deprecated.This API is ICU internal only.private void
readObject(java.io.ObjectInputStream stream)
void
setContext(DisplayContext context)
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.
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
LOCAL_PATTERN_CACHE
private static ICUCache<java.lang.String,java.util.Map<java.lang.String,DateIntervalInfo.PatternInfo>> LOCAL_PATTERN_CACHE
-
fInfo
private DateIntervalInfo fInfo
-
fDateFormat
private SimpleDateFormat fDateFormat
-
fFromCalendar
private Calendar fFromCalendar
-
fToCalendar
private Calendar fToCalendar
-
fSkeleton
private java.lang.String fSkeleton
-
isDateIntervalInfoDefault
private boolean isDateIntervalInfoDefault
-
fIntervalPatterns
private transient java.util.Map<java.lang.String,DateIntervalInfo.PatternInfo> fIntervalPatterns
Interval patterns for this instance's locale.
-
fDatePattern
private java.lang.String fDatePattern
-
fTimePattern
private java.lang.String fTimePattern
-
fDateTimeFormat
private java.lang.String fDateTimeFormat
-
fCapitalizationSetting
private DisplayContext fCapitalizationSetting
-
-
Constructor Detail
-
DateIntervalFormat
private DateIntervalFormat()
-
DateIntervalFormat
@Deprecated public DateIntervalFormat(java.lang.String skeleton, DateIntervalInfo dtItvInfo, SimpleDateFormat simpleDateFormat)
Deprecated.This API is ICU internal only.Construct a DateIntervalFormat from DateFormat, a DateIntervalInfo, and skeleton. DateFormat provides the timezone, calendar, full pattern, and date format symbols information. It should be a SimpleDateFormat object which has a pattern in it. the DateIntervalInfo provides the interval patterns.- Parameters:
skeleton
- the skeleton of the date formatterdtItvInfo
- the DateIntervalInfo object to be adopted.simpleDateFormat
- will be used for formatting
-
DateIntervalFormat
private DateIntervalFormat(java.lang.String skeleton, ULocale locale, SimpleDateFormat simpleDateFormat)
-
-
Method Detail
-
getInstance
public static final DateIntervalFormat getInstance(java.lang.String skeleton)
Construct a DateIntervalFormat from skeleton and the defaultFORMAT
locale. This is a convenient override of getInstance(String skeleton, ULocale locale) with the value of locale as defaultFORMAT
locale.- Parameters:
skeleton
- the skeleton on which interval format based.- Returns:
- a date time interval formatter.
- See Also:
ULocale.Category.FORMAT
-
getInstance
public static final DateIntervalFormat getInstance(java.lang.String skeleton, java.util.Locale locale)
Construct a DateIntervalFormat from skeleton and a given locale. This is a convenient override of getInstance(String skeleton, ULocale locale)Example code:{@.jcite com.ibm.icu.samples.text.dateintervalformat.DateIntervalFormatSample:---dtitvfmtPreDefinedExample}
- Parameters:
skeleton
- the skeleton on which interval format based.locale
- the given locale- Returns:
- a date time interval formatter.
-
getInstance
public static final DateIntervalFormat getInstance(java.lang.String skeleton, ULocale locale)
Construct a DateIntervalFormat from skeleton and a given 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,
- Parameters:
skeleton
- the skeleton on which interval format based.locale
- the given locale- Returns:
- a date time interval formatter.
-
getInstance
public static final DateIntervalFormat getInstance(java.lang.String skeleton, DateIntervalInfo dtitvinf)
Construct a DateIntervalFormat from skeleton DateIntervalInfo, and the defaultFORMAT
locale. This is a convenient override of getInstance(String skeleton, ULocale locale, DateIntervalInfo dtitvinf) with the locale value as defaultFORMAT
locale.- Parameters:
skeleton
- the skeleton on which interval format based.dtitvinf
- the DateIntervalInfo object to be adopted.- Returns:
- a date time interval formatter.
- See Also:
ULocale.Category.FORMAT
-
getInstance
public static final DateIntervalFormat getInstance(java.lang.String skeleton, java.util.Locale locale, DateIntervalInfo dtitvinf)
Construct a DateIntervalFormat from skeleton a DateIntervalInfo, and the given locale. This is a convenient override of getInstance(String skeleton, ULocale locale, DateIntervalInfo dtitvinf)Example code:{@.jcite com.ibm.icu.samples.text.dateintervalformat.DateIntervalFormatSample:---dtitvfmtCustomizedExample}
- Parameters:
skeleton
- the skeleton on which interval format based.locale
- the given localedtitvinf
- the DateIntervalInfo object to be adopted.- Returns:
- a date time interval formatter.
-
getInstance
public static final DateIntervalFormat getInstance(java.lang.String skeleton, ULocale locale, DateIntervalInfo dtitvinf)
Construct a DateIntervalFormat from skeleton a DateIntervalInfo, and the given locale.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}"
- Parameters:
skeleton
- the skeleton on which interval format based.locale
- the given localedtitvinf
- the DateIntervalInfo object to be adopted.- Returns:
- a date time interval formatter.
-
clone
public java.lang.Object clone()
Clone this Format object polymorphically.- Overrides:
clone
in classjava.text.Format
- Returns:
- A copy of the object.
-
format
public final java.lang.StringBuffer format(java.lang.Object obj, java.lang.StringBuffer appendTo, java.text.FieldPosition fieldPosition)
Format an object to produce a string. This method handles Formattable objects with a DateInterval type. If a the Formattable object type is not a DateInterval, IllegalArgumentException is thrown.- Specified by:
format
in classjava.text.Format
- Parameters:
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.- Returns:
- Reference to 'appendTo' parameter.
- Throws:
java.lang.IllegalArgumentException
- if the formatted object is not DateInterval object
-
format
public final java.lang.StringBuffer format(DateInterval dtInterval, java.lang.StringBuffer appendTo, java.text.FieldPosition fieldPosition)
Format a DateInterval to produce a string.- Parameters:
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.- Returns:
- Reference to 'appendTo' parameter.
-
formatToValue
public DateIntervalFormat.FormattedDateInterval formatToValue(DateInterval dtInterval)
Format a DateInterval to produce a FormattedDateInterval. The FormattedDateInterval exposes field information about the formatted string.- Parameters:
dtInterval
- DateInterval to be formatted.- Returns:
- A FormattedDateInterval containing the format result.
-
formatIntervalImpl
private java.lang.StringBuffer formatIntervalImpl(DateInterval dtInterval, java.lang.StringBuffer appendTo, java.text.FieldPosition pos, DateIntervalFormat.FormatOutput output, java.util.List<java.text.FieldPosition> attributes)
-
getPatterns
@Deprecated public java.lang.String getPatterns(Calendar fromCalendar, Calendar toCalendar, Output<java.lang.String> part2)
Deprecated.This API is ICU internal only.
-
format
public final java.lang.StringBuffer format(Calendar fromCalendar, Calendar toCalendar, java.lang.StringBuffer appendTo, java.text.FieldPosition pos)
Format 2 Calendars to produce a string.- Parameters:
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.- Returns:
- Reference to 'appendTo' parameter.
- Throws:
java.lang.IllegalArgumentException
- if the two calendars are not equivalent.
-
format
public final java.lang.StringBuffer format(java.time.temporal.Temporal fromTemporal, java.time.temporal.Temporal toTemporal, java.lang.StringBuffer appendTo, java.text.FieldPosition pos)
Format twoTemporal
s to produce a string.- Parameters:
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.- Returns:
- Reference to 'appendTo' parameter.
- Throws:
java.lang.IllegalArgumentException
- if the two calendars are not equivalent.
-
formatToValue
public DateIntervalFormat.FormattedDateInterval formatToValue(Calendar fromCalendar, Calendar toCalendar)
Format 2 Calendars to produce a FormattedDateInterval. The FormattedDateInterval exposes field information about the formatted string.- Parameters:
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 string- Returns:
- A FormattedDateInterval containing the format result.
-
formatToValue
public DateIntervalFormat.FormattedDateInterval formatToValue(java.time.temporal.Temporal fromTemporal, java.time.temporal.Temporal toTemporal)
Format twoTemporal
s to produce a FormattedDateInterval. The FormattedDateInterval exposes field information about the formatted string.- Parameters:
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- Returns:
- A FormattedDateInterval containing the format result.
-
formatImpl
private java.lang.StringBuffer formatImpl(Calendar fromCalendar, Calendar toCalendar, java.lang.StringBuffer appendTo, java.text.FieldPosition pos, DateIntervalFormat.FormatOutput output, java.util.List<java.text.FieldPosition> attributes)
-
fallbackFormatRange
private final void fallbackFormatRange(Calendar fromCalendar, Calendar toCalendar, java.lang.StringBuffer appendTo, java.lang.StringBuilder patternSB, java.text.FieldPosition pos, DateIntervalFormat.FormatOutput output, java.util.List<java.text.FieldPosition> attributes)
Like fallbackFormat, but specifically for ranges.
-
fallbackFormat
private final java.lang.StringBuffer fallbackFormat(Calendar fromCalendar, Calendar toCalendar, boolean fromToOnSameDay, java.lang.StringBuffer appendTo, java.text.FieldPosition pos, DateIntervalFormat.FormatOutput output, java.util.List<java.text.FieldPosition> attributes)
-
fallbackFormat
private final java.lang.StringBuffer fallbackFormat(Calendar fromCalendar, Calendar toCalendar, boolean fromToOnSameDay, java.lang.StringBuffer appendTo, java.text.FieldPosition pos, DateIntervalFormat.FormatOutput output, java.util.List<java.text.FieldPosition> attributes, java.lang.String fullPattern)
-
parseObject
@Deprecated public java.lang.Object parseObject(java.lang.String source, java.text.ParsePosition parse_pos)
Deprecated.This API is ICU internal only.Date interval parsing is not supported.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.
- Specified by:
parseObject
in classjava.text.Format
- Parameters:
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.- Returns:
- A newly created
Formattable
object, or NULL on failure.
-
getDateIntervalInfo
public DateIntervalInfo getDateIntervalInfo()
Gets the date time interval patterns.- Returns:
- a copy of the date time interval patterns associated with this date interval formatter.
-
setDateIntervalInfo
public void setDateIntervalInfo(DateIntervalInfo newItvPattern)
Set the date time interval patterns.- Parameters:
newItvPattern
- the given interval patterns to copy.
-
getTimeZone
public TimeZone getTimeZone()
Get the TimeZone- Returns:
- A copy of the TimeZone associated with this date interval formatter.
-
setTimeZone
public void setTimeZone(TimeZone zone)
Set the TimeZone for the calendar used by this DateIntervalFormat object.- Parameters:
zone
- The new TimeZone, will be cloned for use by this DateIntervalFormat.
-
setContext
public void setContext(DisplayContext context)
Set a particular DisplayContext value in the formatter, such as CAPITALIZATION_FOR_STANDALONE. This causes the formatted result to be capitalized appropriately for the context in which it is intended to be used, considering both the locale and the type of field at the beginning of the formatted result.- Parameters:
context
- The DisplayContext value to set.
-
getContext
public DisplayContext getContext(DisplayContext.Type type)
Get the formatter's DisplayContext value for the specified DisplayContext.Type, such as CAPITALIZATION.- Parameters:
type
- the DisplayContext.Type whose value to return- Returns:
- the current DisplayContext setting for the specified type
-
getDateFormat
public DateFormat getDateFormat()
Gets the date formatter- Returns:
- a copy of the date formatter associated with this date interval formatter.
-
initializePattern
private void initializePattern(ICUCache<java.lang.String,java.util.Map<java.lang.String,DateIntervalInfo.PatternInfo>> cache)
-
initializeIntervalPattern
private java.util.Map<java.lang.String,DateIntervalInfo.PatternInfo> initializeIntervalPattern(java.lang.String fullPattern, ULocale locale)
-
getConcatenationPattern
private java.lang.String getConcatenationPattern(ULocale locale)
Retrieves the concatenation DateTime pattern from the resource bundle.- Parameters:
locale
- Locale to retrieve.- Returns:
- Concatenation DateTime pattern.
-
genFallbackPattern
private void genFallbackPattern(int field, java.lang.String skeleton, java.util.Map<java.lang.String,DateIntervalInfo.PatternInfo> intervalPatterns, DateTimePatternGenerator dtpng)
-
normalizeHourMetacharacters
private java.lang.String normalizeHourMetacharacters(java.lang.String skeleton, ULocale locale)
-
getDateTimeSkeleton
private static void getDateTimeSkeleton(java.lang.String skeleton, java.lang.StringBuilder dateSkeleton, java.lang.StringBuilder normalizedDateSkeleton, java.lang.StringBuilder timeSkeleton, java.lang.StringBuilder normalizedTimeSkeleton)
-
genSeparateDateTimePtn
private boolean genSeparateDateTimePtn(java.lang.String dateSkeleton, java.lang.String timeSkeleton, java.util.Map<java.lang.String,DateIntervalInfo.PatternInfo> intervalPatterns, DateTimePatternGenerator dtpng)
-
genIntervalPattern
private DateIntervalFormat.SkeletonAndItsBestMatch genIntervalPattern(int field, java.lang.String skeleton, java.lang.String bestSkeleton, int differenceInfo, java.util.Map<java.lang.String,DateIntervalInfo.PatternInfo> intervalPatterns)
-
adjustFieldWidth
private static java.lang.String adjustFieldWidth(java.lang.String inputSkeleton, java.lang.String bestMatchSkeleton, java.lang.String bestMatchIntervalPattern, int differenceInfo, boolean suppressDayPeriodField)
-
findReplaceInPattern
private static java.lang.String findReplaceInPattern(java.lang.String targetString, java.lang.String strToReplace, java.lang.String strToReplaceWith)
Does the same thing as String.replace(), except that it won't perform the substitution inside quoted literal text.- Parameters:
targetString
- The string to perform the find-replace operation on.strToReplace
- The string to search for and replace in the target string.strToReplaceWith
- The string to substitute in whereverstringToReplace
was found.
-
concatSingleDate2TimeInterval
private void concatSingleDate2TimeInterval(java.lang.String dtfmt, java.lang.String datePattern, int field, java.util.Map<java.lang.String,DateIntervalInfo.PatternInfo> intervalPatterns)
-
fieldExistsInSkeleton
private static boolean fieldExistsInSkeleton(int field, java.lang.String skeleton)
-
readObject
private void readObject(java.io.ObjectInputStream stream) throws java.io.IOException, java.lang.ClassNotFoundException
- Throws:
java.io.IOException
java.lang.ClassNotFoundException
-
getRawPatterns
@Deprecated public java.util.Map<java.lang.String,DateIntervalInfo.PatternInfo> getRawPatterns()
Deprecated.This API is ICU internal only.Get the internal patterns for the skeleton
-
-