public class DateTimePatternGenerator
extends Object implements Freezable<DateTimePatternGenerator>, Cloneable
| java.lang.Object | |
| android.icu.text.DateTimePatternGenerator | |
该类提供灵活的日期格式模式生成,如“yy-MM-dd”。 用户可以通过添加连续的模式来建立发生器。 完成后,可以使用“骨架”进行查询,该“骨架”是一种只包含所需字段和长度的模式。 生成器将返回对应于该骨架的“最佳拟合”模式。
人们使用的主要方法是getBestPattern(String skeleton),因为通常这个类是用来自特定语言环境的数据预先构建的。 但是,发电机也可以直接从其他数据构建。
Nested classes |
|
|---|---|
class |
DateTimePatternGenerator.PatternInfo PatternInfo为addPattern(...)提供输出参数。 |
常量(Constants) |
|
|---|---|
int |
DAY |
int |
DAYPERIOD |
int |
DAY_OF_WEEK_IN_MONTH |
int |
DAY_OF_YEAR |
int |
ERA |
int |
FRACTIONAL_SECOND |
int |
HOUR |
int |
MATCH_ALL_FIELDS_LENGTH 选项掩码用于强制所有日期和时间字段的宽度。 |
int |
MATCH_HOUR_FIELD_LENGTH 用于强制小时字段宽度的选项掩码。 |
int |
MATCH_NO_OPTIONS 默认选项掩码用于 |
int |
MINUTE |
int |
MONTH |
int |
QUARTER |
int |
SECOND |
int |
WEEKDAY |
int |
WEEK_OF_MONTH |
int |
WEEK_OF_YEAR |
int |
YEAR |
int |
ZONE |
Protected constructors |
|
|---|---|
DateTimePatternGenerator() 仅供子类使用 |
|
公共方法(Public methods) |
|
|---|---|
DateTimePatternGenerator |
addPattern(String pattern, boolean override, DateTimePatternGenerator.PatternInfo returnInfo) 向发生器添加一个模式。 |
Object |
clone() 返回此 |
DateTimePatternGenerator |
cloneAsThawed() 提供克隆操作。 |
DateTimePatternGenerator |
freeze() 冻结对象。 |
String |
getAppendItemFormat(int field) Getter对应于setAppendItemFormats。 |
String |
getAppendItemName(int field) getter对应于setAppendItemNames。 |
String |
getBaseSkeleton(String pattern) 实用程序从给定模式返回唯一的基本框架。 |
Set<String> |
getBaseSkeletons(Set<String> result) 从这个类中返回所有基础骨架的列表(以规范形式) |
String |
getBestPattern(String skeleton, int options) 返回匹配输入框架的最佳模式。 |
String |
getBestPattern(String skeleton) 返回匹配输入框架的最佳模式。 |
String |
getDateTimeFormat() Getter对应于setDateTimeFormat。 |
String |
getDecimal() getter对应于setDecimal。 |
static DateTimePatternGenerator |
getEmptyInstance() 创建空的生成器,使用addPattern(...)构建 |
static DateTimePatternGenerator |
getInstance() 根据默认的 |
static DateTimePatternGenerator |
getInstance(Locale locale) 根据给定语言环境的数据构建灵活的生成器。 |
static DateTimePatternGenerator |
getInstance(ULocale uLocale) 根据给定语言环境的数据构建灵活的生成器。 |
String |
getSkeleton(String pattern) 从给定模式返回唯一骨架的实用程序。 |
Map<String, String> |
getSkeletons(Map<String, String> result) 从这个类中返回所有骨架的列表(以规范形式),以及它们映射到的模式。 |
boolean |
isFrozen() 确定对象是否被冻结。 |
String |
replaceFieldTypes(String pattern, String skeleton) 调整模式的字段类型(宽度和子类型)以匹配骨架中的内容。 |
String |
replaceFieldTypes(String pattern, String skeleton, int options) 调整模式的字段类型(宽度和子类型)以匹配骨架中的内容。 |
void |
setAppendItemFormat(int field, String value) AppendItem格式是用于在没有良好匹配的情况下附加字段的模式。 |
void |
setAppendItemName(int field, String value) 设置字段的名称,例如英文中的“era”用于ERA。 |
void |
setDateTimeFormat(String dateTimeFormat) 日期时间格式是用于组成日期和时间模式的消息格式模式。 |
void |
setDecimal(String decimal) 十进制值用于格式化秒的分数。 |
继承方法(Inherited methods) |
|
|---|---|
java.lang.Object
|
|
android.icu.util.Freezable
|
|
int MATCH_ALL_FIELDS_LENGTH
选项掩码用于强制所有日期和时间字段的宽度。
常量值:65535(0x0000ffff)
int MATCH_HOUR_FIELD_LENGTH
用于强制小时字段宽度的选项掩码。
常量值:2048(0x00000800)
int MATCH_NO_OPTIONS
默认选项掩码用于 getBestPattern(String, int)和 replaceFieldTypes(String, String, int) 。
常量值:0(0x00000000)
DateTimePatternGenerator addPattern (String pattern, boolean override, DateTimePatternGenerator.PatternInfo returnInfo)
向发生器添加一个模式。 如果模式与现有模式具有相同的骨架,并且设置了覆盖参数,则覆盖之前的值。 否则,保留以前的值。 无论哪种情况,冲突信息都将在PatternInfo中返回。
请注意,单字段模式(如“MMM”)会自动添加,并且不需要明确添加! *
示例代码:
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
*************************************************************************************************/
| 参数(Parameters) | |
|---|---|
pattern |
String: Pattern to add. |
override |
boolean: When existing values are to be overridden use true, otherwise use false. |
returnInfo |
DateTimePatternGenerator.PatternInfo: Returned information. |
| 返回(Returns) | |
|---|---|
DateTimePatternGenerator |
|
Object clone ()
返回此 DateTimePatternGenerator对象的副本。
| 返回(Returns) | |
|---|---|
Object |
A copy of this DateTimePatternGenerator object. |
DateTimePatternGenerator cloneAsThawed ()
提供克隆操作。 任何克隆都是最初解冻的。
| 返回(Returns) | |
|---|---|
DateTimePatternGenerator |
|
DateTimePatternGenerator freeze ()
冻结对象。
| 返回(Returns) | |
|---|---|
DateTimePatternGenerator |
the object itself. |
String getAppendItemFormat (int field)
Getter对应于setAppendItemFormats。 低于0或处于或高于TYPE_LIMIT的值是非法参数。
| 参数(Parameters) | |
|---|---|
field |
int: The index to retrieve the append item formats. |
| 返回(Returns) | |
|---|---|
String |
append pattern for field |
String getAppendItemName (int field)
getter对应于setAppendItemNames。 低于0或处于或高于TYPE_LIMIT的值是非法参数。
| 参数(Parameters) | |
|---|---|
field |
int: The index to get the append item name. |
| 返回(Returns) | |
|---|---|
String |
name for field |
String getBaseSkeleton (String pattern)
实用程序从给定模式返回唯一的基本框架。 这与骨架相同,只是长度差异被最小化,以便仅保留字符串和数字形式之间的差异。 例如,“MMM-dd”和“d / MMM”都会生成骨架“MMMd”(注意单个d)。
| 参数(Parameters) | |
|---|---|
pattern |
String: Input pattern, such as "dd/MMM" |
| 返回(Returns) | |
|---|---|
String |
skeleton, such as "MMMdd" |
Set<String> getBaseSkeletons (Set<String> result)
从这个类中返回所有基础骨架的列表(以规范形式)
| 参数(Parameters) | |
|---|---|
result |
Set
|
| 返回(Returns) | |
|---|---|
Set<String> |
|
String getBestPattern (String skeleton, int options)
返回匹配输入框架的最佳模式。 它保证有骨架中的所有字段。
| 参数(Parameters) | |
|---|---|
skeleton |
String: The skeleton is a pattern containing only the variable fields. For example, "MMMdd" and "mmhh" are skeletons. |
options |
int: 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. |
| 返回(Returns) | |
|---|---|
String |
Best pattern matching the input skeleton (and options). |
String getBestPattern (String skeleton)
返回匹配输入框架的最佳模式。 它保证有骨架中的所有字段。
示例代码:
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日
************************************************************************************************************/
| 参数(Parameters) | |
|---|---|
skeleton |
String: The skeleton is a pattern containing only the variable fields. For example, "MMMdd" and "mmhh" are skeletons. |
| 返回(Returns) | |
|---|---|
String |
Best pattern matching the input skeleton. |
String getDateTimeFormat ()
Getter对应于setDateTimeFormat。
| 返回(Returns) | |
|---|---|
String |
pattern |
String getDecimal ()
getter对应于setDecimal。
| 返回(Returns) | |
|---|---|
String |
string corresponding to the decimal point |
DateTimePatternGenerator getEmptyInstance ()
创建空的生成器,使用addPattern(...)构建
| 返回(Returns) | |
|---|---|
DateTimePatternGenerator |
|
DateTimePatternGenerator getInstance ()
根据默认的 FORMAT语言环境的数据构建一个灵活的生成器。
| 返回(Returns) | |
|---|---|
DateTimePatternGenerator |
|
也可以看看:
DateTimePatternGenerator getInstance (Locale locale)
根据给定语言环境的数据构建灵活的生成器。
| 参数(Parameters) | |
|---|---|
locale |
Locale: The Locale to pass. |
| 返回(Returns) | |
|---|---|
DateTimePatternGenerator |
|
DateTimePatternGenerator getInstance (ULocale uLocale)
根据给定语言环境的数据构建灵活的生成器。
| 参数(Parameters) | |
|---|---|
uLocale |
ULocale: The locale to pass. |
| 返回(Returns) | |
|---|---|
DateTimePatternGenerator |
|
String getSkeleton (String pattern)
从给定模式返回唯一骨架的实用程序。 例如,“MMM-dd”和“dd / MMM”都产生骨架“MMMdd”。
| 参数(Parameters) | |
|---|---|
pattern |
String: Input pattern, such as "dd/MMM" |
| 返回(Returns) | |
|---|---|
String |
skeleton, such as "MMMdd" |
Map<String, String> getSkeletons (Map<String, String> result)
从这个类中返回所有骨架的列表(以规范形式),以及它们映射到的模式。
| 参数(Parameters) | |
|---|---|
result |
Map: 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. 问题:一个替代的API将只是返回一个骨架列表,然后有一个单独的例程来从骨架到模式。 |
| 返回(Returns) | |
|---|---|
Map<String, String> |
the input Map containing the values. |
String replaceFieldTypes (String pattern, String skeleton)
调整模式的字段类型(宽度和子类型)以匹配骨架中的内容。 也就是说,如果提供“dM H:m”之类的图案和“MMMMddhhmm”的骨架,则将输入图案调整为“dd-MMMM hh:mm”。 这用于内部获取输入框架的最佳匹配,但也可以在外部使用。
示例代码:
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
**************************************************************************************************/
| 参数(Parameters) | |
|---|---|
pattern |
String: input pattern |
skeleton |
String: For the pattern to match to. |
| 返回(Returns) | |
|---|---|
String |
pattern adjusted to match the skeleton fields widths and subtypes. |
String replaceFieldTypes (String pattern, String skeleton, int options)
调整模式的字段类型(宽度和子类型)以匹配骨架中的内容。 也就是说,如果提供“dM H:m”之类的图案和“MMMMddhhmm”的骨架,则将输入图案调整为“dd-MMMM hh:mm”。 这用于内部获取输入框架的最佳匹配,但也可以在外部使用。
| 参数(Parameters) | |
|---|---|
pattern |
String: input pattern |
skeleton |
String: For the pattern to match to. |
options |
int: 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. |
| 返回(Returns) | |
|---|---|
String |
pattern adjusted to match the skeleton fields widths and subtypes. |
void setAppendItemFormat (int field,
String value)
AppendItem格式是用于在没有良好匹配的情况下附加字段的模式。 例如,假设输入框架是“GyyyyMMMd”,并且内部没有匹配模式,但是存在与“yyyyMMMd”匹配的模式,例如“d-MM-yyyy”。 然后使用该模式,再加上G.这两者联合的方式是使用AppendItemFormat for G(era)。 所以如果这个值是“{0},{1}”,那么最终的结果模式是“d-MM-yyyy,G”。
实际上有三个可用的变量:{0}是迄今为止的模式,{1}是我们要添加的元素,{2}是元素的名称。
这反映了CLDR数据的组织方式。
| 参数(Parameters) | |
|---|---|
field |
int: such as ERA |
value |
String: pattern, such as "{0}, {1}" |
void setAppendItemName (int field,
String value)
设置字段的名称,例如英文中的“era”用于ERA。 这些仅在使用相应的AppendItemFormat时使用,并且如果它包含一个{2}变量。
这反映了CLDR数据的组织方式。
| 参数(Parameters) | |
|---|---|
field |
int: Index of the append item names. |
value |
String: The value to set the item to. |
void setDateTimeFormat (String dateTimeFormat)
日期时间格式是用于组成日期和时间模式的消息格式模式。 默认值为“{1} {0}”,其中{1}将被日期模式替换,{0}将被时间模式替换。
当输入框架既包含日期字段又包含时间字段时使用,但添加的模式之间没有紧密匹配。 例如,假设此对象是通过添加“dd-MMM”和“hh:mm”创建的,并且其datetimeFormat是默认的“{1} {0}”。 然后,如果输入骨架是“MMMdhmm”,则没有完全匹配,因此输入骨架被分解为两个组件“MMMd”和“hmm”。 这两个骷髅有近似的匹配,所以结果与这种模式相结合,导致“d-MMM h:mm”。
| 参数(Parameters) | |
|---|---|
dateTimeFormat |
String: message format pattern, where {1} will be replaced by the date pattern and {0} will be replaced by the time pattern. |
void setDecimal (String decimal)
十进制值用于格式化秒的分数。 如果骨架包含小数秒,那么这与小数秒一起使用。 例如,假设输入模式为“hhmmssSSSS”,内部最佳匹配模式为“H:mm:ss”,十进制字符串为“,”。 然后将结果图案修改为“H:mm:ss,SSSS”
| 参数(Parameters) | |
|---|---|
decimal |
String: The decimal to set to. |