虽然我无法重现您描述的行为,但可以向您展示如何控制格式化程序使用的数字。
NumberFormat nf = NumberFormat.getNumberInstance();
if (nf instanceof DecimalFormat) {
DecimalFormat df = (DecimalFormat) nf;
// Hindi digits
char ch0 = '\u0966'; // Hindi zero
DecimalFormatSymbols symbols = df.getDecimalFormatSymbols();
symbols.setZeroDigit(ch0);
df.setDecimalFormatSymbols(symbols);
System.out.println(nf.format(53.54));
// Western digits
symbols.setZeroDigit('0'); // Western zero
df.setDecimalFormatSymbols(symbols);
System.out.println(nf.format(53.54));
} else {
System.out.println("Cannot control digits for this formatter");
}
在我的计算机上,此代码段的输出是:
५३.५४
53.54
我们仅指定零位数字,即用于0的字符。格式化程序会从中指出其他数字本身。
对于格式化日期,我非常热烈地推荐java.time,现代的Java日期和时间API及其DateTimeFormatter
。
DateTimeFormatter dateFormatter = DateTimeFormatter
.ofLocalizedDate(FormatStyle.SHORT).withLocale(Locale.GERMAN);
// Hindi digits
DecimalStyle style = dateFormatter.getDecimalStyle().withZeroDigit(ch0);
DateTimeFormatter hindiDateFormatter = dateFormatter.withDecimalStyle(style);
System.out.println(LocalDate.now(ZoneId.of("Europe/Vienna")).format(hindiDateFormatter));
// Western digits
style = dateFormatter.getDecimalStyle().withZeroDigit('0');
DateTimeFormatter westernDateFormatter = dateFormatter.withDecimalStyle(style);
System.out.println(LocalDate.now(ZoneId.of("Europe/Zurich")).format(westernDateFormatter));
१२.१२.१९
12.12.19
众所周知,您在问题中尝试使用的DateFormat
类很麻烦,而且已经过时了。
链接: Oracle tutorial: Date Time解释了如何使用java.time。
本文链接:https://www.f2er.com/2946862.html