0

I'm having trouble with a complex corporate web application which has been deployed in a French branch and now has to be deployed also in a German branch of the company. The problem has to do with the different behaviour of DateFormat based on the locale of the server:

The Date for United States:
  In FULL is Tuesday, January 16, 2007
  In LONG is January 16, 2007
  In MEDIUM is Jan 16, 2007
  In SHORT is 1/16/07
The Date for United Kingdom:
  In FULL is 16 January 2007
  In LONG is 16 January 2007
  In MEDIUM is 16-Jan-2007
  In SHORT is 16/01/07
The Date for Germany:
  In FULL is Dienstag, 16. Januar 2007
  In LONG is 16. Januar 2007
  In MEDIUM is 16.01.2007
  In SHORT is 16.01.07
The Date for France:
  In FULL is mardi 16 janvier 2007
  In LONG is 16 janvier 2007
  In MEDIUM is 16 janv. 2007
  In SHORT is 16/01/07

Now the application makes extensive use of:

 brch.setDate(DateFormat.getDateInstance(DateFormat.SHORT).parse("02/04/2013"));

the above seems to work in France and the UK, however when it is deployed in Germany all hell breaks loose and we receive tons of errors. Now I know we can specify the Locale with a

 DateFormat.getDateInstance(DateFormat.SHORT, Locale.UK)

however changing the code for the whole application would require days and a lot of testing, is there a way to specify globally the default locale for this application? Or some way to make the applicaion Locale-agnostic without changing too much the application?

Thank you

4

1 回答 1

2

有没有办法为这个应用程序全局指定默认语言环境?

您可以Locale.setDefault在程序开始时调用,为使用它的所有内容强制使用默认语言环境 - 包括DateFormat.getDateInstance.

但是,我强烈建议您无论如何都要重构代码。您所要做的就是更改当前用于DateFormat.getDateInstance调用受您控制的实用程序方法的每个代码。随着需求的变化,您可以轻松地在一个地方集中更改行为。作为第一步,即使只是简单的搜索和替换也可能允许您相当容易地将其提取到受控类中的静态方法中。

是的,这需要一些时间——但之后你会过得更好

就个人而言,我建议如果你实际上有一个非常固定的格式,你明确指定 - 如果你知道它总是dd/MM/yyyy,我会使用:

DateFormat format = new SimpleDateFormat("dd/MM/yyyy", Locale.US);

...然后也适当地设置时区。事实上,我会使用 Joda Time 而不是其中的任何一个,但那是另一回事。

于 2013-05-23T12:25:02.853 回答