简体   繁体   中英

Decimal point (.) as separator in java application and comma (,) separator in Oracle database

The problem is not so easy to fix.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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