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

dendini
  • 3,842
  • 9
  • 37
  • 74
  • Consider this previous answer. http://stackoverflow.com/questions/8809098/how-do-i-set-the-default-locale-for-my-jvm – Chris Rickey May 23 '13 at 12:25

1 Answers1

2

is there a way to specify globally the default locale for this application?

You could call Locale.setDefault at the start of the program, to force the default locale for everything that uses it - including DateFormat.getDateInstance.

However, I would strongly advise you to refactor the code anyway. All you've got to do is change every code which currently uses DateFormat.getDateInstance to call a utility method which is under your control. You can then easily change the behaviour in one place centrally, as your requirements change. Even just a plain search and replace would probably allow you to extract this to a static method in a controlled class fairly easily, as a first step.

Yes, this will take a bit of time - but you'll be much better off after it.

Personally, I would recommend that if you've actually got a very fixed format, you specify that explicitly - if you know it's always dd/MM/yyyy, I'd use:

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

... and then set the time zone appropriately too. In fact, I'd use Joda Time instead of any of this, but that's a different matter.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • The start of the program is the problem :) it's like 9 projects put somehow together with Maven, also adding the "-Duser.country=US -Duser.language=en" as a compile option in NetBeans is not enough because tests are run without the option and they break the compilation. – dendini May 23 '13 at 12:28
  • @dendini: Well there must be *some* sort of entry point. Most web containers allow initialization on a per-app basis. It does sound like as soon as you've fixed the initial problem you should work to make the code more maintainable... (Your tests would need to set the locale too, of course.) Fundamentally you're discovering that if your codebase is a mess, it's hard to change things - there's no easy way out of that, I'm afraid. – Jon Skeet May 23 '13 at 12:29
  • I think the DateFormat class is wrongly conceived as it doesn't use a default Locale equal for everyone and on specific request use any available Locale, it does instead the exact opposite: use the machine's Locale and only on user request use another one, this of course is troublesome for applications where a user doesn't mind specifying a Locale and the thing comes out when the same project is used in another country! – dendini May 23 '13 at 13:23
  • @dendini: While I don't personally like defaulting a locale either, the documentation is pretty clear on it. I don't think you can really blame `DateFormat` for the mess your application is in. – Jon Skeet May 23 '13 at 14:23