下面列出了java.time.format.DateTimeFormatterBuilder#getLocalizedDateTimePattern ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* generateDefaultFormatterBCE, This returns a default formatter for the specified locale, that
* can be used for displaying or parsing BC dates. The formatter is generated from the default
* FormatStyle.LONG formatter in the specified locale. The resulting format is intended to be
* nearly identical to the default formatter used for AD dates.
*/
public static DateTimeFormatter generateDefaultFormatterBCE(Locale pickerLocale) {
// This is verified to work for the following locale languages:
// en, de, fr, pt, ru, it, nl, es, pl, da, ro, sv, zh.
String displayFormatterBCPattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(
FormatStyle.LONG, null, IsoChronology.INSTANCE, pickerLocale);
displayFormatterBCPattern = displayFormatterBCPattern.replace("y", "u");
// Note: We could have used DateUtilities.createFormatterFromPatternString(), which should
// have the same formatter options as this line. We kept this code independent in case
// anyone ever mistakenly changes that utility function.
DateTimeFormatter displayFormatterBC = new DateTimeFormatterBuilder().parseLenient()
.parseCaseInsensitive().appendPattern(displayFormatterBCPattern)
.toFormatter(pickerLocale);
// Get the local language as a string.
String language = pickerLocale.getLanguage();
// Override the format for the turkish locale to remove the name of the weekday.
if ("tr".equals(language)) {
displayFormatterBC = PickerUtilities.createFormatterFromPatternString(
"dd MMMM uuuu", pickerLocale);
}
return displayFormatterBC;
}
@Override
public DateTimeFormatter getDateTimeFormatterWithSeconds() {
String dateFormatPattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(FormatStyle.SHORT, FormatStyle.MEDIUM, IsoChronology.INSTANCE, getLocale());
dateFormatPattern = dateFormatPattern.replaceFirst("y+", "yyyy").replaceFirst(", ", " ");
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(dateFormatPattern, getLocale());
dateTimeFormatter.withZone(TimeZone.getTimeZone(getAdminTimezone()).toZoneId());
dateTimeFormatter.withResolverStyle(ResolverStyle.STRICT);
return dateTimeFormatter;
}
@Override
public DateTimeFormatter getDateTimeFormatter() {
String dateTimeFormatPattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(FormatStyle.SHORT, FormatStyle.SHORT, IsoChronology.INSTANCE, getLocale());
dateTimeFormatPattern = dateTimeFormatPattern.replaceFirst("y+", "yyyy").replaceFirst(", ", " ");
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(dateTimeFormatPattern, getLocale());
dateTimeFormatter.withZone(TimeZone.getTimeZone(getAdminTimezone()).toZoneId());
dateTimeFormatter.withResolverStyle(ResolverStyle.STRICT);
return dateTimeFormatter;
}
@Test
@UseDataProvider("localizedDateTimePatterns")
public void test_getLocalizedDateTimePattern(FormatStyle dateStyle, FormatStyle timeStyle,
Chronology chrono, Locale locale, String expected) {
String actual = DateTimeFormatterBuilder.getLocalizedDateTimePattern(dateStyle, timeStyle, chrono, locale);
assertEquals("Pattern " + convertNonAscii(actual), actual, expected);
}
@Test(dataProvider="localePatterns")
public void test_getLocalizedDateTimePattern(FormatStyle dateStyle, FormatStyle timeStyle,
Chronology chrono, Locale locale, String expected) {
String actual = DateTimeFormatterBuilder.getLocalizedDateTimePattern(dateStyle, timeStyle, chrono, locale);
assertEquals(actual, expected, "Pattern " + convertNonAscii(actual));
}
@Test(expectedExceptions=java.lang.NullPointerException.class)
public void test_getLocalizedChronoNPE() {
DateTimeFormatterBuilder.getLocalizedDateTimePattern(FormatStyle.SHORT, FormatStyle.SHORT, null, Locale.US);
}
@Test(expectedExceptions=java.lang.NullPointerException.class)
public void test_getLocalizedChronoNPE() {
DateTimeFormatterBuilder.getLocalizedDateTimePattern(FormatStyle.SHORT, FormatStyle.SHORT, null, Locale.US);
}
@Test(expectedExceptions=java.lang.IllegalArgumentException.class)
public void test_getLocalizedDateTimePatternIAE() {
DateTimeFormatterBuilder.getLocalizedDateTimePattern(null, null, IsoChronology.INSTANCE, Locale.US);
}
@Test(dataProvider="localePatterns")
public void test_getLocalizedDateTimePattern(FormatStyle dateStyle, FormatStyle timeStyle,
Chronology chrono, Locale locale, String expected) {
String actual = DateTimeFormatterBuilder.getLocalizedDateTimePattern(dateStyle, timeStyle, chrono, locale);
assertEquals(actual, expected, "Pattern " + convertNonAscii(actual));
}
@Test(expectedExceptions=java.lang.NullPointerException.class)
public void test_getLocalizedLocaleNPE() {
DateTimeFormatterBuilder.getLocalizedDateTimePattern(FormatStyle.SHORT, FormatStyle.SHORT, IsoChronology.INSTANCE, null);
}
@Test(expectedExceptions=java.lang.NullPointerException.class)
public void test_getLocalizedLocaleNPE() {
DateTimeFormatterBuilder.getLocalizedDateTimePattern(FormatStyle.SHORT, FormatStyle.SHORT, IsoChronology.INSTANCE, null);
}
@Test(expectedExceptions=java.lang.NullPointerException.class)
public void test_getLocalizedLocaleNPE() {
DateTimeFormatterBuilder.getLocalizedDateTimePattern(FormatStyle.SHORT, FormatStyle.SHORT, IsoChronology.INSTANCE, null);
}
@Test(dataProvider="localePatterns")
public void test_getLocalizedDateTimePattern(FormatStyle dateStyle, FormatStyle timeStyle,
Chronology chrono, Locale locale, String expected) {
String actual = DateTimeFormatterBuilder.getLocalizedDateTimePattern(dateStyle, timeStyle, chrono, locale);
assertEquals(actual, expected, "Pattern " + convertNonAscii(actual));
}
@Test(expectedExceptions=java.lang.NullPointerException.class)
public void test_getLocalizedChronoNPE() {
DateTimeFormatterBuilder.getLocalizedDateTimePattern(FormatStyle.SHORT, FormatStyle.SHORT, null, Locale.US);
}
@Test(dataProvider="localePatterns")
public void test_getLocalizedDateTimePattern(FormatStyle dateStyle, FormatStyle timeStyle,
Chronology chrono, Locale locale, String expected) {
String actual = DateTimeFormatterBuilder.getLocalizedDateTimePattern(dateStyle, timeStyle, chrono, locale);
assertEquals(actual, expected, "Pattern " + convertNonAscii(actual));
}
@Test(dataProvider="localePatterns")
public void test_getLocalizedDateTimePattern(FormatStyle dateStyle, FormatStyle timeStyle,
Chronology chrono, Locale locale, String expected) {
String actual = DateTimeFormatterBuilder.getLocalizedDateTimePattern(dateStyle, timeStyle, chrono, locale);
assertEquals(actual, expected, "Pattern " + convertNonAscii(actual));
}
@Test(expectedExceptions=java.lang.NullPointerException.class)
public void test_getLocalizedChronoNPE() {
DateTimeFormatterBuilder.getLocalizedDateTimePattern(FormatStyle.SHORT, FormatStyle.SHORT, null, Locale.US);
}
@Test(expectedExceptions=java.lang.NullPointerException.class)
public void test_getLocalizedLocaleNPE() {
DateTimeFormatterBuilder.getLocalizedDateTimePattern(FormatStyle.SHORT, FormatStyle.SHORT, IsoChronology.INSTANCE, null);
}
@Test(dataProvider="localePatterns")
public void test_getLocalizedDateTimePattern(FormatStyle dateStyle, FormatStyle timeStyle,
Chronology chrono, Locale locale, String expected) {
String actual = DateTimeFormatterBuilder.getLocalizedDateTimePattern(dateStyle, timeStyle, chrono, locale);
assertEquals(actual, expected, "Pattern " + convertNonAscii(actual));
}
@Nonnull
public static String getPatternTime (@Nonnull final FormatStyle eStyle, @Nonnull final Locale aDisplayLocale)
{
return DateTimeFormatterBuilder.getLocalizedDateTimePattern (null, eStyle, IsoChronology.INSTANCE, aDisplayLocale);
}