如何在不设置区域设置的情况下设置 NLS 日期格式
我们的java类调用PLSQL proc,它以NLS_DATE_FORMAT定义的默认格式返回日期。我们的应用程序设置了自己的国际化区域设置,但我希望日期格式保留为“DD-MON-RR”,即 en_US 区域设置 NLS_DATE_FORMAT。由于语言环境的更改,oracle 获取的日期字符串有所不同,并且后续的 TO_DATE() 函数调用失败。我尝试通过将 Locale 更改为 Locale.setDefault(new Locale("en","US")); 来解决此问题//"en_US" 在 java 类中,它工作正常,但国际化部分不再工作。我在新加坡,所以我的区域设置是 "en_SG"
,设置 NLS_TERRITORY: SINGAPORE
后 oracle 采用的日期格式是 NLS_DATE_FORMAT:'DD/MM/RR'
。我向服务器查询 V$NLS_PARAMETERS,默认日期格式为“DD-MON-RR”。所以我的问题是,我可以在不影响应用程序的区域设置的情况下设置 NLS_DATE_FORMAT 吗?或者我可以让 jdbc 驱动程序完全忽略客户端的 NLS 设置吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
to_char
函数根据需要格式化日期,也可以在to_char
函数中包含第三个参数来表示所使用的区域设置:可以找到有关日期格式的更多信息在SQL 参考中。
Yo can use
to_char
function to format the date as you want, also you can include the third parameter into_char
function to denote locale used:More info about date formats can be found in SQL Reference.
我正在为我的应用程序敲定 i18n 内容,我的设计基于以下假设)
1) Locale.setDefault(new Locale("en","US"));这很糟糕,因为您正在更改 JVM 的默认区域设置,而不仅仅是您的应用程序的默认区域设置。因此,最好在每个请求的基础上传递应用程序的语言环境(可能是线程本地的)
2) 在应用程序级别处理所有 i18n 格式化/解析。 DB 应该只处理比较(可选)、排序(可选)和存储。因此只需设置 NLS_CHARACTERSET、NLS_COMP、NLS_SORT 和 NLS_LENGTH_SEMANTICS。顺便说一句,数据库级别的比较和排序意味着创建特定于区域设置的索引,从而减慢插入/更新速度
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