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

StackOverflow https://stackoverflow.com/questions/20659096

Domanda

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.

I have created the locale objects and their constructors as shown below. I have also created a NumberFormat and a DateFormat instance.

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. 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?

È stato utile?

Soluzione

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 dateFormat = new SimpleDateFormat("dd/MM/yyyy", aLocale);     
 System.out.println(dateFormat.parse("12/03/1993"));

Altri suggerimenti

For the date portion, consider using 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.

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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top