简体   繁体   English

如何使用DateFormat和NumberFormat为几个不同的语言环境输出相同的日期,数字,价格和百分比?

[英]How to use DateFormat and NumberFormat to output the same date, number, price and percentage for a few different Locales?

I have used Calender.getAvailableLocales() method to find which locales are available and from that have chosen 4. For each of these I want to output the same date, number, price and percentage to show the differences between the locales. 我使用了Calender.getAvailableLocales()方法来查找可用的语言环境,并从中选择了4个。我要为每个语言环境输出相同的日期,数字,价格和百分比,以显示语言环境之间的差异。

I have created the locale objects and their constructors as shown below. 我已经创建了语言环境对象及其构造函数,如下所示。 I have also created a NumberFormat and a DateFormat instance. 我还创建了一个NumberFormat和一个DateFormat实例。

Locale aLocale = new Locale.Builder().setLanguage("fr").setRegion("CA").build();
Locale bLocale = new Locale.Builder().setLanguage("en").setRegion("US").build();
Locale cLocale = new Locale.Builder().setLanguage("en").setRegion("GB").build();
Locale dLocale = new Locale.Builder().setLanguage("ru").setScript("Cyrl").build();

aLocale = new Locale("fr", "CA");
bLocale = new Locale("en", "US");
cLocale = new Locale("en", "GB");
dLocale = new Locale("ru");

NumberFormat nf = NumberFormat.getInstance();
DateFormat df = DateFormat.getInstance();

The problem I am having is how I actually make the method call to the NumberFormat to output the various values I want. 我遇到的问题是我实际上如何对NumberFormat进行方法调用以输出所需的各种值。 I assume I need to use the instance I have created somehow with the locales that I created but I cannot figure out how. 我假设我需要将自己创建的实例与我创建的语言环境一起使用,但是我不知道该怎么做。 Any help will be greatly appreciated. 任何帮助将不胜感激。 Just a pointer and not a whole solution please. 请只是一个指针,而不是一个完整的解决方案。

Based on the suggestions made, I am now using this style to show the differences. 根据提出的建议,我现在使用这种样式来显示差异。

NumberFormat aNumber = NumberFormat.getNumberInstance(aLocale);
NumberFormat aPercent = NumberFormat.getPercentInstance(aLocale);
NumberFormat aPrice = NumberFormat.getCurrencyInstance(aLocale);

NumberFormat bNumber = NumberFormat.getNumberInstance(bLocale);
NumberFormat bPercent = NumberFormat.getPercentInstance(bLocale);
NumberFormat bPrice = NumberFormat.getCurrencyInstance(bLocale);


System.out.println(aNumber.format(9257476));
System.out.println(aPercent.format(63));
System.out.println(aPrice.format(684));

System.out.println(bNumber.format(9257476));
System.out.println(bPercent.format(63));
System.out.println(bPrice.format(684));

Which outputs this: 输出以下内容:

9 257 476
6 300 %
684,00 $
9,257,476
6,300%
$684.00

I tried doing a similar thing for the date but the method doesn't allow for a locale to be passed as a parameter. 我尝试对日期进行类似的操作,但是该方法不允许将语言环境作为参数传递。 I need to make sure the correct locale is being used each time so I'm not sure how to adjust for this? 我需要确保每次使用的语言环境都正确,因此我不确定如何对此进行调整?

Are you looking for this. 您在寻找这个吗?

NumberFormat currency = NumberFormat.getCurrencyInstance(aLocale);
NumberFormat percent = NumberFormat.getPercentInstance(aLocale);
NumberFormat number = NumberFormat.getNumberInstance(aLocale);

int style = DateFormat.MEDIUM;
DateFormat df = DateFormat.getInstance(style,aLocale);

eg 例如

NumberFormat format = NumberFormat.getCurrencyInstance();
Number number = format.parse("$19,67,456.45");
System.out.println(number.toString());      
System.out.println(format.format(Double.valueOf(number.toString())));

output 产量

1967456.45
$1,967,456.45

Edit 编辑

Use SimpleDateFormat for date with specified format. 将SimpleDateFormat用于具有指定格式的日期。

 SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy", aLocale);     
 System.out.println(dateFormat.parse("12/03/1993"));

For the date portion, consider using Joda-Time . 对于日期部分,请考虑使用Joda-Time

Your question did not say exactly what you are doing with dates or date-times, but perhaps my answer will point in the right direction. 您的问题并未确切说明您对日期或日期时间在做什么,但也许我的答案将指向正确的方向。

If using a date-time (such as java.util.Date) rather than literally a date without time, be aware of time zones. 如果使用日期时间(例如java.util.Date)而不是字面上没有时间的日期,请注意时区。

And do not confuse time zones with locales. 并且不要将时区与语言环境混淆。 To that end, my example code purposely uses India time zone while formatting output in both French and United States styles. 为此,我的示例代码在使用法语和美国风格格式化输出时,特意使用了印度时区。

// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;
// import org.joda.time.format.*;

DateTimeFormatter formatter = DateTimeFormat.forPattern( "yyyy/MM/dd" );
String string = "2013/12/17";

// Assuming the date is in Kolkata (formerly known as Calcutta India).
DateTimeZone kolkataTimeZone = DateTimeZone.forID( "Asia/Kolkata" );
DateTime dateTimeInKolkata = formatter.withZone( kolkataTimeZone ).parseDateTime( string ).withTimeAtStartOfDay();
// For a French person in Puducherry/Pondicherry India.
String dateTimeTextInKolkataInFrench = DateTimeFormat.shortDate().withLocale( Locale.FRENCH ).print( dateTimeInKolkata );
// To compare, United States style.
String dateTimeTextInKolkataInUnitedStatesStyle = DateTimeFormat.shortDate().withLocale( Locale.US ).print( dateTimeInKolkata );

Dump to console… 转储到控制台...

System.out.println( "dateTimeInKolkata: " + dateTimeInKolkata ); // Default ISO 8601 format.
System.out.println( "En français: " + dateTimeTextInKolkataInFrench );
System.out.println( "US style: " + dateTimeTextInKolkataInUnitedStatesStyle );

When run… 运行时...

dateTimeInKolkata: 2013-12-17T00:00:00.000+05:30
En français: 17/12/13
US style: 12/17/13

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM