views:

31

answers:

2

Our java class calls PLSQL proc which returns date in default format which is defined by NLS_DATE_FORMAT. Our application sets its own Locale for internationalization but I want the date format to remain just 'DD-MON-RR' which is en_US Locale NLS_DATE_FORMAT. Due to the change in locale oracle's fetched Date string differs and subsequent TO_DATE() function calls are failing. I tried fixing this by changing Locale to Locale.setDefault(new Locale("en","US")); //"en_US" in java class and it works fine, but the internationalization part wont work anymore. I am in Singapore so my Locale is "en_SG" and the date format that oracle assumes after setting NLS_TERRITORY: SINGAPORE is NLS_DATE_FORMAT:'DD/MM/RR'. I queries server for V$NLS_PARAMETERS and there the default date format is 'DD-MON-RR'. So my question is, can i set the NLS_DATE_FORMAT without affecting Locale settings of application. Or can I make jdbc driver to ignore NLS settings of client altogether?

A: 

Yo can use to_char function to format the date as you want, also you can include the third parameter in to_char function to denote locale used:

select to_char(sysdate, 'DY-MM-YYYY', 'NLS_DATE_LANGUAGE=AMERICAN')  from dual;

More info about date formats can be found in SQL Reference.

andr
Oh yes, actually I could have used `TO_CHAR(sysdate,'DD-MON-RR')` in PLSQL if it was possible in my case. But my project is re-engineering a part of old PRO C* code into java. So everyone wants to keep the PL/SQL parts unchanged as it currently works fine. The problem occurred as PRO C* code lies in the same machine as Database so they follow the same NLS settings. But this Java class might be part of application server and in future its most likely to move into a web service. Hence I want to keep the existing Locale settings as the application also works fine currently.
samarjit samanta
A: 

I am kind of finalizing on the i18n stuff for my application and my design is based on below assumptions)

1) Locale.setDefault(new Locale("en","US")); is bad as you are changing the default locale for the JVM and not just for your app. So it is better to pass the locale around the app on a per request basis (may be threadlocal)

2) Handle all i18n formatting/parsing at the application level. DB should only handle comparisions (optionally), sorting (optionally) and storage. So just set the NLS_CHARACTERSET, NLS_COMP, NLS_SORT and NLS_LENGTH_SEMANTICS. By the way comparision and sorting at the db level means creating locale specific indexes thus slowing down the inserts/updates

Pangea
Yes your first point is true. For the second point we do have i18n formatting in front end, but the procedure returns data in the from of string from oracle procedure. A simpler code how the string is formed is like `select 'MKDT'||lpad(nvl(length(por.maker_date),0),3,'0')||por.maker_date data from TABLEXYZ;` I guess this line automatically calls `TO_CHAR(<DATE>, <DEFAULT NLS DATE FORMAT>)` and converts the date. I know they should have used `TO_CHAR(<DATE>, <SPECIFIC DATE FORMAT>)`. I have already suggested doing this change in DB Procs, but it involves a lot of changes to stable PROCS.
samarjit samanta