public class DateTimePatternGenerator extends Object implements Freezable<DateTimePatternGenerator>, Cloneable
The main method people will use is getBestPattern(String skeleton), since normally this class is pre-built with data from a particular locale. However, generators can be built directly from other data as well.
Modifier and Type | Class and Description |
---|---|
static class |
DateTimePatternGenerator.DisplayWidth
Field display name width constants for getFieldDisplayName
|
static class |
DateTimePatternGenerator.FormatParser
Deprecated.
This API is ICU internal only.
|
static class |
DateTimePatternGenerator.PatternInfo
PatternInfo supplies output parameters for addPattern(...).
|
static class |
DateTimePatternGenerator.VariableField
Deprecated.
This API is ICU internal only.
|
Modifier and Type | Field and Description |
---|---|
static int |
DAY |
static int |
DAY_OF_WEEK_IN_MONTH |
static int |
DAY_OF_YEAR |
static int |
DAYPERIOD |
static int |
ERA |
static int |
FRACTIONAL_SECOND |
static int |
HOUR |
static int |
MATCH_ALL_FIELDS_LENGTH
Option mask for forcing the width of all date and time fields.
|
static int |
MATCH_HOUR_FIELD_LENGTH
Option mask for forcing the width of hour field.
|
static int |
MATCH_MINUTE_FIELD_LENGTH
Deprecated.
This API is ICU internal only.
|
static int |
MATCH_NO_OPTIONS
Default option mask used for
getBestPattern(String, int)
and replaceFieldTypes(String, String, int) . |
static int |
MATCH_SECOND_FIELD_LENGTH
Deprecated.
This API is ICU internal only.
|
static int |
MINUTE |
static int |
MONTH |
static int |
QUARTER |
static int |
SECOND |
static int |
TYPE_LIMIT
Deprecated.
ICU 58 The numeric value may change over time, see ICU ticket #12420.
|
static int |
WEEK_OF_MONTH |
static int |
WEEK_OF_YEAR |
static int |
WEEKDAY |
static int |
YEAR |
static int |
ZONE |
Modifier | Constructor and Description |
---|---|
protected |
DateTimePatternGenerator()
Only for use by subclasses
|
Modifier and Type | Method and Description |
---|---|
DateTimePatternGenerator |
addPattern(String pattern,
boolean override,
DateTimePatternGenerator.PatternInfo returnInfo)
Adds a pattern to the generator.
|
DateTimePatternGenerator |
addPatternWithSkeleton(String pattern,
String skeletonToUse,
boolean override,
DateTimePatternGenerator.PatternInfo returnInfo)
Deprecated.
This API is ICU internal only.
|
Object |
clone()
Returns a copy of this
DateTimePatternGenerator object. |
DateTimePatternGenerator |
cloneAsThawed()
Provides for the clone operation.
|
DateTimePatternGenerator |
freeze()
Freezes the object.
|
static int |
getAppendFormatNumber(String string)
Deprecated.
This API is ICU internal only.
|
static int |
getAppendFormatNumber(com.ibm.icu.impl.UResource.Key key)
Deprecated.
This API is ICU internal only.
|
String |
getAppendItemFormat(int field)
Getter corresponding to setAppendItemFormats.
|
String |
getAppendItemName(int field)
Getter corresponding to setAppendItemName.
|
String |
getBaseSkeleton(String pattern)
Utility to return a unique base skeleton from a given pattern.
|
Set<String> |
getBaseSkeletons(Set<String> result)
Return a list of all the base skeletons (in canonical form) from this class
|
String |
getBestPattern(String skeleton)
Return the best pattern matching the input skeleton.
|
String |
getBestPattern(String skeleton,
int options)
Return the best pattern matching the input skeleton.
|
String |
getCanonicalSkeletonAllowingDuplicates(String pattern)
Deprecated.
This API is ICU internal only.
|
String |
getDateTimeFormat()
Getter corresponding to setDateTimeFormat.
|
String |
getDateTimeFormat(int style)
Getter corresponding to setDateTimeFormat.
|
String |
getDecimal()
Getter corresponding to setDecimal.
|
DateFormat.HourCycle |
getDefaultHourCycle()
Return the default hour cycle.
|
char |
getDefaultHourFormatChar()
Deprecated.
This API is ICU internal only.
|
static DateTimePatternGenerator |
getEmptyInstance()
Create empty generator, to be constructed with addPattern(...) etc.
|
String |
getFieldDisplayName(int field,
DateTimePatternGenerator.DisplayWidth width)
The general interface to get a display name for a particular date/time field,
in one of several possible display widths.
|
String |
getFields(String pattern)
Deprecated.
This API is ICU internal only.
|
static DateTimePatternGenerator |
getFrozenInstance(ULocale uLocale)
Deprecated.
This API is ICU internal only.
|
static DateTimePatternGenerator |
getInstance()
Construct a flexible generator according to data for the default
FORMAT locale. |
static DateTimePatternGenerator |
getInstance(Locale locale)
Construct a flexible generator according to data for a given locale.
|
static DateTimePatternGenerator |
getInstance(ULocale uLocale)
Construct a flexible generator according to data for a given locale.
|
static DateTimePatternGenerator |
getInstanceNoStdPat(ULocale uLocale)
Deprecated.
This API is ICU internal only.
|
Collection<String> |
getRedundants(Collection<String> output)
Deprecated.
This API is ICU internal only.
|
String |
getSkeleton(String pattern)
Utility to return a unique skeleton from a given pattern.
|
String |
getSkeletonAllowingDuplicates(String pattern)
Deprecated.
This API is ICU internal only.
|
Map<String,String> |
getSkeletons(Map<String,String> result)
Return a list of all the skeletons (in canonical form) from this class,
and the patterns that they map to.
|
boolean |
isFrozen()
Determines whether the object has been frozen or not.
|
static boolean |
isSingleField(String skeleton)
Deprecated.
This API is ICU internal only.
|
String |
replaceFieldTypes(String pattern,
String skeleton)
Adjusts the field types (width and subtype) of a pattern to match what is
in a skeleton.
|
String |
replaceFieldTypes(String pattern,
String skeleton,
int options)
Adjusts the field types (width and subtype) of a pattern to match what is
in a skeleton.
|
void |
setAppendItemFormat(int field,
String value)
An AppendItem format is a pattern used to append a field if there is no
good match.
|
void |
setAppendItemName(int field,
String value)
Sets the names of fields, eg "era" in English for ERA.
|
void |
setDateTimeFormat(int style,
String dateTimeFormat)
dateTimeFormats are message patterns used to compose combinations of date
and time patterns.
|
void |
setDateTimeFormat(String dateTimeFormat)
The date time format is a message format pattern used to compose date and
time patterns.
|
void |
setDecimal(String decimal)
The decimal value is used in formatting fractions of seconds.
|
void |
setDefaultHourFormatChar(char defaultHourFormatChar)
Deprecated.
This API is ICU internal only.
|
boolean |
skeletonsAreSimilar(String id,
String skeleton)
Deprecated.
This API is ICU internal only.
|
public static final int ERA
public static final int YEAR
public static final int QUARTER
public static final int MONTH
public static final int WEEK_OF_YEAR
public static final int WEEK_OF_MONTH
public static final int WEEKDAY
public static final int DAY
public static final int DAY_OF_YEAR
public static final int DAY_OF_WEEK_IN_MONTH
public static final int DAYPERIOD
public static final int HOUR
public static final int MINUTE
public static final int SECOND
public static final int FRACTIONAL_SECOND
public static final int ZONE
@Deprecated public static final int TYPE_LIMIT
public static final int MATCH_NO_OPTIONS
getBestPattern(String, int)
and replaceFieldTypes(String, String, int)
.getBestPattern(String, int)
,
replaceFieldTypes(String, String, int)
,
Constant Field Valuespublic static final int MATCH_HOUR_FIELD_LENGTH
getBestPattern(String, int)
,
replaceFieldTypes(String, String, int)
,
Constant Field Values@Deprecated public static final int MATCH_MINUTE_FIELD_LENGTH
@Deprecated public static final int MATCH_SECOND_FIELD_LENGTH
public static final int MATCH_ALL_FIELDS_LENGTH
getBestPattern(String, int)
,
replaceFieldTypes(String, String, int)
,
Constant Field Valuesprotected DateTimePatternGenerator()
public static DateTimePatternGenerator getEmptyInstance()
public static DateTimePatternGenerator getInstance()
FORMAT
locale.ULocale.Category.FORMAT
public static DateTimePatternGenerator getInstance(ULocale uLocale)
uLocale
- The locale to pass.public static DateTimePatternGenerator getInstance(Locale locale)
locale
- The Locale
to pass.@Deprecated public static DateTimePatternGenerator getFrozenInstance(ULocale uLocale)
uLocale
- The locale to pass.@Deprecated public static DateTimePatternGenerator getInstanceNoStdPat(ULocale uLocale)
uLocale
- The locale to pass.@Deprecated public char getDefaultHourFormatChar()
@Deprecated public void setDefaultHourFormatChar(char defaultHourFormatChar)
@Deprecated public static int getAppendFormatNumber(com.ibm.icu.impl.UResource.Key key)
@Deprecated public static int getAppendFormatNumber(String string)
public String getBestPattern(String skeleton)
Example code:
import java.util.Date; import com.ibm.icu.text.DateFormat; import com.ibm.icu.text.DateTimePatternGenerator; import com.ibm.icu.text.SimpleDateFormat; import com.ibm.icu.util.GregorianCalendar; import com.ibm.icu.util.TimeZone; import com.ibm.icu.util.ULocale; final String[] skeletons = { "yQQQQ", // year + full name of quarter, i.e., 4th quarter 1999 "yMMMM", // year + full name of month, i.e., October 1999 "MMMMd", // full name of month + day of the month, i.e., October 25 "hhmm", // 12-hour-cycle format, i.e., 1:32 PM "jjmm" // preferred hour format for the given locale, i.e., 24-hour-cycle format for fr_FR }; final ULocale[] locales = { new ULocale ("en_US"), new ULocale ("fr_FR"), new ULocale ("zh_CN"), }; DateTimePatternGenerator dtfg = null; Date date= new GregorianCalendar(1999,9,13,23,58,59).getTime(); System.out.printf("%-20s%-35s%-35s%-35s\n\n", "Skeleton", "en_US", "fr_FR","zh_CN"); for (String skeleton:skeletons) { System.out.printf("%-20s", skeleton); for (ULocale locale:locales) { // create a DateTimePatternGenerator instance for given locale dtfg = DateTimePatternGenerator.getInstance(locale); // use getBestPattern method to get the best pattern for the given skeleton String pattern = dtfg.getBestPattern(skeleton); // Constructs a SimpleDateFormat with the best pattern generated above and the given locale SimpleDateFormat sdf = new SimpleDateFormat(pattern, locale); // Get the format of the given date System.out.printf("%-35s",sdf.format(date)); } System.out.println("\n"); } /** output of the sample code: ************************************************************************************************************* Skeleton en_US fr_FR zh_CN yQQQQ 4th quarter 1999 4e trimestre 1999 1999年第四季度 yMMMM October 1999 octobre 1999 1999年10月 MMMMd October 13 13 octobre 10月13日 hhmm 11:58 PM 11:58 PM 下午11:58 jjmm 11:58 PM 23:58 下午11:58 **************************************************************************************************************/ // Use DateTime.getPatternInstance to produce the same Date/Time format with predefined constant field value final String[] dtfskeleton = { DateFormat.YEAR_QUARTER, // year + full name of quarter, i.e., 4th quarter 1999 DateFormat.YEAR_MONTH, // year + full name of month, i.e., October 1999 DateFormat.MONTH_DAY // full name of month + day of the month, i.e., October 25 }; System.out.printf("%-20s%-35s%-35s%-35s\n\n", "Skeleton", "en_US", "fr_FR","zh_CN"); for (String skeleton:dtfskeleton) { System.out.printf("%-20s", skeleton); for (ULocale locale:locales) { // Use DateFormat.getPatternInstance to get the date/time format for the locale, // and apply the format to the given date String df=DateFormat.getPatternInstance(skeleton,locale).format(date); System.out.printf("%-35s",df); } System.out.println("\n"); } /** output of the sample code: ************************************************************************************************************ Skeleton en_US fr_FR zh_CN yQQQQ 4th quarter 1999 4e trimestre 1999 1999年第四季度 yMMMM October 1999 octobre 1999 1999年10月 MMMMd October 13 13 octobre 10月13日 ************************************************************************************************************/
skeleton
- The skeleton is a pattern containing only the variable fields.
For example, "MMMdd" and "mmhh" are skeletons.public String getBestPattern(String skeleton, int options)
skeleton
- The skeleton is a pattern containing only the variable fields.
For example, "MMMdd" and "mmhh" are skeletons.options
- MATCH_xxx options for forcing the length of specified fields in
the returned pattern to match those in the skeleton (when this would
not happen otherwise). For default behavior, use MATCH_NO_OPTIONS.public DateTimePatternGenerator addPattern(String pattern, boolean override, DateTimePatternGenerator.PatternInfo returnInfo)
Note that single-field patterns (like "MMM") are automatically added, and don't need to be added explicitly! *
Example code:
Date date= new GregorianCalendar(1999,9,13,23,58,59).getTime(); ULocale locale = ULocale.FRANCE; // Create an DateTimePatternGenerator instance for the given locale DateTimePatternGenerator gen = DateTimePatternGenerator.getInstance(locale); SimpleDateFormat format = new SimpleDateFormat(gen.getBestPattern("MMMMddHmm"), locale); DateTimePatternGenerator.PatternInfo returnInfo = new DateTimePatternGenerator.PatternInfo(); // Add '. von' to the existing pattern gen.addPattern("dd'. von' MMMM", true, returnInfo); // Apply the new pattern format.applyPattern(gen.getBestPattern("MMMMddHmm")); System.out.println("New Pattern for FRENCH: "+format.toPattern()); System.out.println("Date Time in new Pattern: "+format.format(date)); /** output of the sample code: ************************************************************************************************** New Pattern for FRENCH: dd. 'von' MMMM HH:mm Date Time in new Pattern: 13. von octobre 23:58 *************************************************************************************************/
pattern
- Pattern to add.override
- When existing values are to be overridden use true, otherwise
use false.returnInfo
- Returned information.@Deprecated public DateTimePatternGenerator addPatternWithSkeleton(String pattern, String skeletonToUse, boolean override, DateTimePatternGenerator.PatternInfo returnInfo)
public String getSkeleton(String pattern)
pattern
- Input pattern, such as "dd/MMM"@Deprecated public String getSkeletonAllowingDuplicates(String pattern)
pattern
- Input pattern, such as "dd/MMM"@Deprecated public String getCanonicalSkeletonAllowingDuplicates(String pattern)
pattern
- Input pattern, such as "ccc, d LLL"public String getBaseSkeleton(String pattern)
pattern
- Input pattern, such as "dd/MMM"public Map<String,String> getSkeletons(Map<String,String> result)
result
- an output Map in which to place the mapping from skeleton to
pattern. If you want to see the internal order being used,
supply a LinkedHashMap. If the input value is null, then a
LinkedHashMap is allocated.
Issue: an alternate API would be to just return a list of the skeletons, and then have a separate routine to get from skeleton to pattern.
public Set<String> getBaseSkeletons(Set<String> result)
public String replaceFieldTypes(String pattern, String skeleton)
Example code:
Date date= new GregorianCalendar(1999,9,13,23,58,59).getTime(); TimeZone zone = TimeZone.getTimeZone("Europe/Paris"); ULocale locale = ULocale.FRANCE; DateTimePatternGenerator gen = DateTimePatternGenerator.getInstance(locale); SimpleDateFormat format = new SimpleDateFormat("EEEE d MMMM y HH:mm:ss zzzz",locale); format.setTimeZone(zone); String pattern = format.toPattern(); System.out.println("Pattern before replacement:"); System.out.println(pattern); System.out.println("Date/Time format in fr_FR:"); System.out.println(format.format(date)); // Replace zone "zzzz" in the pattern with "vvvv" String newPattern = gen.replaceFieldTypes(pattern, "vvvv"); // Apply the new pattern format.applyPattern(newPattern); System.out.println("Pattern after replacement:"); System.out.println(newPattern); System.out.println("Date/Time format in fr_FR:"); System.out.println(format.format(date)); /** output of the sample code: *************************************************************************************************** Pattern before replacement: EEEE d MMMM y HH:mm:ss zzzz Date/Time format in fr_FR: jeudi 14 octobre 1999 05:58:59 heure avancée d’Europe centrale Pattern after replacement: EEEE d MMMM y HH:mm:ss vvvv Date/Time format in fr_FR: jeudi 14 octobre 1999 05:58:59 heure de l’Europe centrale **************************************************************************************************/
pattern
- input patternskeleton
- For the pattern to match to.public String replaceFieldTypes(String pattern, String skeleton, int options)
pattern
- input patternskeleton
- For the pattern to match to.options
- MATCH_xxx options for forcing the length of specified fields in
the returned pattern to match those in the skeleton (when this would
not happen otherwise). For default behavior, use MATCH_NO_OPTIONS.public void setDateTimeFormat(String dateTimeFormat)
This is used when the input skeleton contains both date and time fields, but there is not a close match among the added patterns. For example, suppose that this object was created by adding "dd-MMM" and "hh:mm", and its datetimeFormat is the default "{1} {0}". Then if the input skeleton is "MMMdhmm", there is not an exact match, so the input skeleton is broken up into two components "MMMd" and "hmm". There are close matches for those two skeletons, so the result is put together with this pattern, resulting in "d-MMM h:mm". There are four DateTimeFormats in a DateTimePatternGenerator object, corresponding to date styles DateFormat.FULL..DateFormat.SHORT. This method sets all of them to the specified pattern. To set them individually, see setDateTimeFormat(int style, ...).
dateTimeFormat
- message format pattern, where {1} will be replaced by the date
pattern and {0} will be replaced by the time pattern.public String getDateTimeFormat()
public void setDateTimeFormat(int style, String dateTimeFormat)
style
- one of DateFormat.FULL..DateFormat.SHORT. An exception will
be thrown if out of range.dateTimeFormat
- the new dateTimeFormat to set for the specified stylepublic String getDateTimeFormat(int style)
style
- one of DateFormat.FULL..DateFormat.SHORT. An exception will
be thrown if out of range.public void setDecimal(String decimal)
decimal
- The decimal to set to.public String getDecimal()
@Deprecated public Collection<String> getRedundants(Collection<String> output)
output
- stores the redundant patterns that are removed. To get these
in internal order, supply a LinkedHashSet. If null, a
collection is allocated.public void setAppendItemFormat(int field, String value)
There are actually three available variables: {0} is the pattern so far, {1} is the element we are adding, and {2} is the name of the element.
This reflects the way that the CLDR data is organized.
field
- such as ERAvalue
- pattern, such as "{0}, {1}"public String getAppendItemFormat(int field)
field
- The index to retrieve the append item formats.public void setAppendItemName(int field, String value)
This reflects the way that the CLDR data is organized.
field
- Index of the append item names.value
- The value to set the item to.public String getAppendItemName(int field)
field
- The index to get the append item name.getFieldDisplayName(int, DisplayWidth)
public DateFormat.HourCycle getDefaultHourCycle()
public String getFieldDisplayName(int field, DateTimePatternGenerator.DisplayWidth width)
field
- The field type, such as ERA.width
- The desired DisplayWidth, such as DisplayWidth.ABBREVIATED.@Deprecated public static boolean isSingleField(String skeleton)
skeleton
- The skeleton to determine if it contains a single field.public boolean isFrozen()
isFrozen
in interface Freezable<DateTimePatternGenerator>
public DateTimePatternGenerator freeze()
freeze
in interface Freezable<DateTimePatternGenerator>
public DateTimePatternGenerator cloneAsThawed()
cloneAsThawed
in interface Freezable<DateTimePatternGenerator>
public Object clone()
DateTimePatternGenerator
object.@Deprecated public boolean skeletonsAreSimilar(String id, String skeleton)
@Deprecated public String getFields(String pattern)
pattern
- The pattern that is passed.Copyright © 2016 Unicode, Inc. and others.