|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.ibm.icu.text.DateTimePatternGenerator
public class DateTimePatternGenerator
This class provides flexible generation of date format patterns, like "yy-MM-dd". The user can build up the generator by adding successive patterns. Once that is done, a query can be made using a "skeleton", which is a pattern which just includes the desired fields and lengths. The generator will return the "best fit" pattern corresponding to that skeleton.
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.
// some simple use cases Date sampleDate = new Date(99, 9, 13, 23, 58, 59); ULocale locale = ULocale.GERMANY; TimeZone zone = TimeZone.getTimeZone("Europe/Paris"); // make from locale DateTimePatternGenerator gen = DateTimePatternGenerator.getInstance(locale); SimpleDateFormat format = new SimpleDateFormat(gen.getBestPattern("MMMddHmm"), locale); format.setTimeZone(zone); assertEquals("simple format: MMMddHmm", "8:58 14. Okt", format.format(sampleDate)); // (a generator can be built from scratch, but that is not a typical use case) // modify the generator by adding patterns DateTimePatternGenerator.PatternInfo returnInfo = new DateTimePatternGenerator.PatternInfo(); gen.add("d'. von' MMMM", true, returnInfo); // the returnInfo is mostly useful for debugging problem cases format.applyPattern(gen.getBestPattern("MMMMddHmm")); assertEquals("modified format: MMMddHmm", "8:58 14. von Oktober", format.format(sampleDate)); // get a pattern and modify it format = (SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, locale); format.setTimeZone(zone); String pattern = format.toPattern(); assertEquals("full-date", "Donnerstag, 14. Oktober 1999 8:58 Uhr GMT+02:00", format.format(sampleDate)); // modify it to change the zone. String newPattern = gen.replaceFieldTypes(pattern, "vvvv"); format.applyPattern(newPattern); assertEquals("full-date, modified zone", "Donnerstag, 14. Oktober 1999 8:58 Uhr Frankreich", format.format(sampleDate));
Nested Class Summary | |
---|---|
static class |
DateTimePatternGenerator.FormatParser
Deprecated. This API is ICU internal only. |
static class |
DateTimePatternGenerator.PatternInfo
PatternInfo supplies output parameters for add(...). |
static class |
DateTimePatternGenerator.VariableField
Deprecated. This API is ICU internal only. |
Field Summary | |
---|---|
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 |
MINUTE
|
static int |
MONTH
|
static int |
QUARTER
|
static int |
SECOND
|
static int |
TYPE_LIMIT
|
static int |
WEEK_OF_MONTH
|
static int |
WEEK_OF_YEAR
|
static int |
WEEKDAY
|
static int |
YEAR
|
static int |
ZONE
|
Constructor Summary | |
---|---|
protected |
DateTimePatternGenerator()
Only for use by subclasses |
Method Summary | |
---|---|
DateTimePatternGenerator |
addPattern(String pattern,
boolean override,
DateTimePatternGenerator.PatternInfo returnInfo)
Adds a pattern to the generator. |
Object |
clone()
Boilerplate |
Object |
cloneAsThawed()
Boilerplate for Freezable |
Object |
freeze()
Boilerplate for Freezable |
String |
getAppendItemFormat(int field)
Getter corresponding to setAppendItemFormats. |
String |
getAppendItemName(int field)
Getter corresponding to setAppendItemNames. |
String |
getBaseSkeleton(String pattern)
Utility to return a unique base skeleton from a given pattern. |
Set |
getBaseSkeletons(Set 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 |
getDateTimeFormat()
Getter corresponding to setDateTimeFormat. |
String |
getDecimal()
Getter corresponding to setDecimal. |
static DateTimePatternGenerator |
getEmptyInstance()
Create empty generator, to be constructed with add(...) etc. |
String |
getFields(String pattern)
Deprecated. This API is ICU internal only. |
static DateTimePatternGenerator |
getInstance()
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. |
Collection |
getRedundants(Collection output)
Deprecated. This API is ICU internal only. |
String |
getSkeleton(String pattern)
Utility to return a unique skeleton from a given pattern. |
Map |
getSkeletons(Map result)
Return a list of all the skeletons (in canonical form) from this class, and the patterns that they map to. |
boolean |
isFrozen()
Boilerplate for Freezable |
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. |
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(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. |
Methods inherited from class java.lang.Object |
---|
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
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
public static final int TYPE_LIMIT
Constructor Detail |
---|
protected DateTimePatternGenerator()
Method Detail |
---|
public static DateTimePatternGenerator getEmptyInstance()
public static DateTimePatternGenerator getInstance()
public static DateTimePatternGenerator getInstance(ULocale uLocale)
uLocale
- public String getBestPattern(String skeleton)
skeleton
- The skeleton is a pattern containing only the variable fields.
For example, "MMMdd" and "mmhh" are skeletons.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!
override
- when existing values are to be overridden use true, otherwise
use false.public String getSkeleton(String pattern)
pattern
- Input pattern, such as "dd/MMM"
public String getBaseSkeleton(String pattern)
pattern
- Input pattern, such as "dd/MMM"
public Map getSkeletons(Map 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 getBaseSkeletons(Set result)
public String replaceFieldTypes(String pattern, String skeleton)
pattern
- input patternskeleton
-
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 "{0} {1}". 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".
dateTimeFormat
- message format pattern, here {0} will be replaced by the date
pattern and {1} will be replaced by the time pattern.public String getDateTimeFormat()
public void setDecimal(String decimal)
decimal
- public String getDecimal()
public Collection getRedundants(Collection 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
-
public void setAppendItemName(int field, String value)
This reflects the way that the CLDR data is organized.
field
- value
- public String getAppendItemName(int field)
field
-
public static boolean isSingleField(String skeleton)
skeleton
-
public boolean isFrozen()
isFrozen
in interface Freezable
public Object freeze()
freeze
in interface Freezable
public Object cloneAsThawed()
cloneAsThawed
in interface Freezable
public Object clone()
clone
in class Object
public String getFields(String pattern)
pattern
-
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |