DateTimePatternGenerator.GetBestPattern 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
| 名稱 | Description |
|---|---|
| GetBestPattern(String, DateTimePatternMatchOptions) |
回傳與輸入骨架最匹配的模式。 |
| GetBestPattern(String) |
回傳與輸入骨架最匹配的模式。 |
GetBestPattern(String, DateTimePatternMatchOptions)
回傳與輸入骨架最匹配的模式。
[Android.Runtime.Register("getBestPattern", "(Ljava/lang/String;I)Ljava/lang/String;", "GetGetBestPattern_Ljava_lang_String_IHandler", ApiSince=24)]
public virtual string? GetBestPattern(string? skeleton, Android.Icu.Text.DateTimePatternMatchOptions options);
[<Android.Runtime.Register("getBestPattern", "(Ljava/lang/String;I)Ljava/lang/String;", "GetGetBestPattern_Ljava_lang_String_IHandler", ApiSince=24)>]
abstract member GetBestPattern : string * Android.Icu.Text.DateTimePatternMatchOptions -> string
override this.GetBestPattern : string * Android.Icu.Text.DateTimePatternMatchOptions -> string
參數
- skeleton
- String
骨架是一個僅包含變數場的模式。 例如,「MMMdd」和「mmhh」是骨架。
- options
- DateTimePatternMatchOptions
MATCH_xxx選項,可以強制回傳模式中指定欄位的長度與骨架中的欄位長度相符(否則不會發生)。 預設行為則用MATCH_NO_OPTIONS。
傳回
最佳的模式與輸入骨架(及選項)相匹配。
- 屬性
備註
回傳與輸入骨架最匹配的模式。 它保證擁有骨架中所有欄位。
Java 文件 android.icu.text.DateTimePatternGenerator.getBestPattern(java.lang.String, int)。
本頁部分內容為基於 Open Source Project 所創建與分享的作品,並依授權條款所描述的使用進行修改。
適用於
GetBestPattern(String)
回傳與輸入骨架最匹配的模式。
[Android.Runtime.Register("getBestPattern", "(Ljava/lang/String;)Ljava/lang/String;", "GetGetBestPattern_Ljava_lang_String_Handler", ApiSince=24)]
public virtual string? GetBestPattern(string? skeleton);
[<Android.Runtime.Register("getBestPattern", "(Ljava/lang/String;)Ljava/lang/String;", "GetGetBestPattern_Ljava_lang_String_Handler", ApiSince=24)>]
abstract member GetBestPattern : string -> string
override this.GetBestPattern : string -> string
參數
- skeleton
- String
骨架是一個僅包含變數場的模式。 例如,「MMMdd」和「mmhh」是骨架。
傳回
最佳的模式與輸入骨架相匹配。
- 屬性
備註
回傳與輸入骨架最匹配的模式。 它保證擁有骨架中所有欄位。
範例程式碼:
import java.util.Date;
import android.icu.text.DateFormat;
import android.icu.text.DateTimePatternGenerator;
import android.icu.text.SimpleDateFormat;
import android.icu.util.GregorianCalendar;
import android.icu.util.TimeZone;
import android.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\u5e74\u7b2c\u56db\u5b63\u5ea6
yMMMM October 1999 octobre 1999 1999\u5e7410\u6708
MMMMd October 13 13 octobre 10\u670813\u65e5
hhmm 11:58 PM 11:58 PM \u4e0b\u534811:58
jjmm 11:58 PM 23:58 \u4e0b\u534811: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\u5e74\u7b2c\u56db\u5b63\u5ea6
yMMMM October 1999 octobre 1999 1999\u5e7410\u6708
MMMMd October 13 13 octobre 10\u670813\u65e5
************************************************************************************************************/
Android 參考資料。android.icu.text.DateTimePatternGenerator.getBestPattern
本頁部分內容為基於 Open Source Project 所創建與分享的作品,並依授權條款所描述的使用進行修改。