下面列出了怎么用java.util.Locale.Category的API类实例代码及写法,或者点击链接到github查看源代码。
static void formatLocaleTest() {
StringBuilder sb = new StringBuilder();
IntStream.range(0, src.size()).forEach(i -> {
sb.setLength(0);
Locale.setDefault(Locale.Category.FORMAT, formatLocale.get(i));
new Formatter(sb).format(conversions.get(i), src.get(i));
if (!sb.toString().equals(expected.get(i))) {
throw new RuntimeException(
"Wrong uppercasing with Formatter.format(" +
"\"" + conversions.get(i) + "\"" +
") in locale "
+ formatLocale.get(i) +
". Expected: " + expected.get(i) +
" Returned: " + sb.toString());
}
});
}
private static void printLocale() {
Locale locale = Locale.getDefault();
ostream.println(LOCALE_SETTINGS);
ostream.println(INDENT + "default locale = " +
locale.getDisplayLanguage());
ostream.println(INDENT + "default display locale = " +
Locale.getDefault(Category.DISPLAY).getDisplayName());
ostream.println(INDENT + "default format locale = " +
Locale.getDefault(Category.FORMAT).getDisplayName());
printLocales();
ostream.println();
}
private static void printLocale() {
Locale locale = Locale.getDefault();
ostream.println(LOCALE_SETTINGS);
ostream.println(INDENT + "default locale = " +
locale.getDisplayLanguage());
ostream.println(INDENT + "default display locale = " +
Locale.getDefault(Category.DISPLAY).getDisplayName());
ostream.println(INDENT + "default format locale = " +
Locale.getDefault(Category.FORMAT).getDisplayName());
printLocales();
ostream.println();
}
private static void printLocale() {
Locale locale = Locale.getDefault();
ostream.println(LOCALE_SETTINGS);
ostream.println(INDENT + "default locale = " +
locale.getDisplayLanguage());
ostream.println(INDENT + "default display locale = " +
Locale.getDefault(Category.DISPLAY).getDisplayName());
ostream.println(INDENT + "default format locale = " +
Locale.getDefault(Category.FORMAT).getDisplayName());
printLocales();
ostream.println();
}
private static void printLocale() {
Locale locale = Locale.getDefault();
ostream.println(LOCALE_SETTINGS);
ostream.println(INDENT + "default locale = " +
locale.getDisplayLanguage());
ostream.println(INDENT + "default display locale = " +
Locale.getDefault(Category.DISPLAY).getDisplayName());
ostream.println(INDENT + "default format locale = " +
Locale.getDefault(Category.FORMAT).getDisplayName());
printLocales();
ostream.println();
}
private static void printLocale() {
Locale locale = Locale.getDefault();
ostream.println(LOCALE_SETTINGS);
ostream.println(INDENT + "default locale = " +
locale.getDisplayLanguage());
ostream.println(INDENT + "default display locale = " +
Locale.getDefault(Category.DISPLAY).getDisplayName());
ostream.println(INDENT + "default format locale = " +
Locale.getDefault(Category.FORMAT).getDisplayName());
printLocales();
ostream.println();
}
private static void printLocale() {
Locale locale = Locale.getDefault();
ostream.println(LOCALE_SETTINGS);
ostream.println(INDENT + "default locale = " +
locale.getDisplayName());
ostream.println(INDENT + "default display locale = " +
Locale.getDefault(Category.DISPLAY).getDisplayName());
ostream.println(INDENT + "default format locale = " +
Locale.getDefault(Category.FORMAT).getDisplayName());
printLocales();
ostream.println();
}
private static void printLocale() {
Locale locale = Locale.getDefault();
ostream.println(LOCALE_SETTINGS);
ostream.println(INDENT + "default locale = " +
locale.getDisplayLanguage());
ostream.println(INDENT + "default display locale = " +
Locale.getDefault(Category.DISPLAY).getDisplayName());
ostream.println(INDENT + "default format locale = " +
Locale.getDefault(Category.FORMAT).getDisplayName());
printLocales();
ostream.println();
}
public static void main(String [] args) {
IntStream.range(0, formatLocale.size()).forEach(i -> {
Locale.setDefault(Locale.Category.FORMAT, formatLocale.get(i));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
new PrintStream(baos).format("%.2f", src);
if (!baos.toString().equals(expected.get(i))) {
throw new RuntimeException(
"Wrong conversion with PrintStream.format() in locale "
+ formatLocale.get(i) +
". Expected: " + expected.get(i) +
" Returned: " + baos.toString());
}
});
}
private static void printLocale() {
Locale locale = Locale.getDefault();
ostream.println(LOCALE_SETTINGS);
ostream.println(INDENT + "default locale = " +
locale.getDisplayLanguage());
ostream.println(INDENT + "default display locale = " +
Locale.getDefault(Category.DISPLAY).getDisplayName());
ostream.println(INDENT + "default format locale = " +
Locale.getDefault(Category.FORMAT).getDisplayName());
printLocales();
ostream.println();
}
private static void printLocale() {
Locale locale = Locale.getDefault();
ostream.println(LOCALE_SETTINGS);
ostream.println(INDENT + "default locale = " +
locale.getDisplayLanguage());
ostream.println(INDENT + "default display locale = " +
Locale.getDefault(Category.DISPLAY).getDisplayName());
ostream.println(INDENT + "default format locale = " +
Locale.getDefault(Category.FORMAT).getDisplayName());
printLocales();
ostream.println();
}
private static void printLocale() {
Locale locale = Locale.getDefault();
ostream.println(LOCALE_SETTINGS);
ostream.println(INDENT + "default locale = " +
locale.getDisplayLanguage());
ostream.println(INDENT + "default display locale = " +
Locale.getDefault(Category.DISPLAY).getDisplayName());
ostream.println(INDENT + "default format locale = " +
Locale.getDefault(Category.FORMAT).getDisplayName());
printLocales();
ostream.println();
}
private static void printLocale() {
Locale locale = Locale.getDefault();
ostream.println(LOCALE_SETTINGS);
ostream.println(INDENT + "default locale = " +
locale.getDisplayLanguage());
ostream.println(INDENT + "default display locale = " +
Locale.getDefault(Category.DISPLAY).getDisplayName());
ostream.println(INDENT + "default format locale = " +
Locale.getDefault(Category.FORMAT).getDisplayName());
printLocales();
ostream.println();
}
private static void printLocale() {
Locale locale = Locale.getDefault();
ostream.println(LOCALE_SETTINGS);
ostream.println(INDENT + "default locale = " +
locale.getDisplayLanguage());
ostream.println(INDENT + "default display locale = " +
Locale.getDefault(Category.DISPLAY).getDisplayName());
ostream.println(INDENT + "default format locale = " +
Locale.getDefault(Category.FORMAT).getDisplayName());
printLocales();
ostream.println();
}
private static void printLocale() {
Locale locale = Locale.getDefault();
ostream.println(LOCALE_SETTINGS);
ostream.println(INDENT + "default locale = " +
locale.getDisplayLanguage());
ostream.println(INDENT + "default display locale = " +
Locale.getDefault(Category.DISPLAY).getDisplayName());
ostream.println(INDENT + "default format locale = " +
Locale.getDefault(Category.FORMAT).getDisplayName());
printLocales();
ostream.println();
}
private static void printLocale() {
Locale locale = Locale.getDefault();
ostream.println(LOCALE_SETTINGS);
ostream.println(INDENT + "default locale = " +
locale.getDisplayLanguage());
ostream.println(INDENT + "default display locale = " +
Locale.getDefault(Category.DISPLAY).getDisplayName());
ostream.println(INDENT + "default format locale = " +
Locale.getDefault(Category.FORMAT).getDisplayName());
printLocales();
ostream.println();
}
@Override
public String interpolate ( final String message, final Context context )
{
return interpolate ( message, context, Locale.getDefault ( Category.DISPLAY ) );
}
@SuppressFBWarnings ("UC_USELESS_OBJECT")
private static void _main_adj (final int repeat, final String name, final double [] arr)
{
long time1;
final MainDoubleToString a = new MainDoubleToString ();
LOGGER.info ("The " + name);
LOGGER.info (" " + Arrays.toString (arr));
LOGGER.info ("are appended to a StringBuilder one by one " + repeat + " times.");
Runtime.getRuntime ().gc ();
LOGGER.info ("Starting test");
time1 = System.currentTimeMillis ();
StringBuilder s1 = new StringBuilder ();
for (int i = repeat; i > 0; i--)
for (int j = arr.length - 1; j >= 0; j--)
a.append (s1, arr[j]);
time1 = System.currentTimeMillis () - time1;
LOGGER.info (" The append took " + time1 + " milliseconds");
s1 = null;
Runtime.getRuntime ().gc ();
LOGGER.info ("Starting test");
time1 = System.currentTimeMillis ();
StringBuilder s2 = new StringBuilder ();
for (int i = repeat; i > 0; i--)
for (int j = arr.length - 1; j >= 0; j--)
a.appendFormatted (s2, arr[j], 4, ',', '.', 3, '-', NO_PREFIX_OR_SUFFIX);
time1 = System.currentTimeMillis () - time1;
LOGGER.info (" The format append took " + time1 + " milliseconds");
s2 = null;
Runtime.getRuntime ().gc ();
LOGGER.info ("Starting test");
StringBuilder s3 = new StringBuilder ();
time1 = System.currentTimeMillis ();
for (int i = repeat; i > 0; i--)
for (int j = arr.length - 1; j >= 0; j--)
s3.append (arr[j]);
time1 = System.currentTimeMillis () - time1;
LOGGER.info (" The StringBuilder took " + time1 + " milliseconds");
s3 = null;
Runtime.getRuntime ().gc ();
StringBuffer sb1 = new StringBuffer ();
LOGGER.info ("Starting test");
time1 = System.currentTimeMillis ();
final DecimalFormat format = new DecimalFormat ("#,##0.0000",
DecimalFormatSymbols.getInstance (Locale.getDefault (Category.FORMAT)));
final FieldPosition f = new FieldPosition (0);
for (int i = repeat; i > 0; i--)
for (int j = arr.length - 1; j >= 0; j--)
format.format (arr[j], sb1, f);
time1 = System.currentTimeMillis () - time1;
LOGGER.info (" The DecimalFormat took " + time1 + " milliseconds");
sb1 = null;
Runtime.getRuntime ().gc ();
s1 = new StringBuilder ();
for (int j = arr.length - 1; j >= 0; j--)
{
if (s1.length () > 0)
s1.append (", ");
a.append (s1, arr[j]);
}
s2 = new StringBuilder ();
for (int j = arr.length - 1; j >= 0; j--)
{
if (s2.length () > 0)
s2.append (", ");
a.appendFormatted (s2, arr[j], 4, ',', '.', 3, '-', NO_PREFIX_OR_SUFFIX);
}
s3 = new StringBuilder ();
time1 = System.currentTimeMillis ();
for (int j = arr.length - 1; j >= 0; j--)
{
if (s3.length () > 0)
s3.append (", ");
s3.append (arr[j]);
}
sb1 = new StringBuffer ();
for (int j = arr.length - 1; j >= 0; j--)
{
if (sb1.length () > 0)
sb1.append (", ");
format.format (arr[j], sb1, f);
}
LOGGER.info (" " + s1);
LOGGER.info (" " + s2);
LOGGER.info (" " + s3);
LOGGER.info (" " + sb1);
LOGGER.info ("");
}