Reputation: 11
The problem is not so easy to fix.
With the following code,
Locale.setDefault(new Locale("fr", "FR"));
the format of a float is 123 456,45 and the date format is 30 mars 2015.
With the following code,
Locale.setDefault(new Locale("en", "US"));
the format of a float is 123,456.45 and the date format is Mar 30, 2015.
How can I set the default Locale
to have 123,456.45 and 30 mars 2015 ? I mean english format for the float and french format for the date and the others properties? Is it possible to set Locale in french format and to change only the decimal separator to point (.)?
The problem I want to solve is the following : I want to call a PL/SQL procedure from java with jdbcTemplate and with float and date parameters. The database is set with point (.) as decimal separator and comma for group. The java application is set with default Locale to french (fr_FR). But when executing the application an error is raised in the PL/SQL procedure because the decimal separators are not the same. The date format is the same in the database and in the java application. I can not change the database settings. How can I do to execute the PL/SQL procedure without errors ?
Thank for your help.
Upvotes: 0
Views: 2118
Reputation: 178303
You don't have to manipulate the default Locale
every time you want to format something. Both the NumberFormat
class and the DateFormat
class have static methods for obtaining numeric and date formatters in a specific locale.
For NumberFormat
, use the getNumberInstance
method that takes a Locale
as a parameter.
double value = 123456.45;
NumberFormat english = NumberFormat.getNumberInstance(new Locale("en", "US"));
System.out.println(english.format(value));
Output:
123,456.45
For DateFormat
, use the getDateInstance
method that takes a Locale
as a parameter.
Date now = new Date();
DateFormat french = DateFormat.getDateInstance(DateFormat.MEDIUM, new Locale("fr", "FR"));
System.out.println(french.format(now));
Output:
30 mars 2015
Upvotes: 1