Search code examples
localejodatime

Can I change default locale to use?


Is it possible to change default locale to use? I don't wanna explicitly tell withLocale each time I need to parse/print date.


Solution

  • Two options:

    The simplest way is to set the locale globally, at the JVM level eg, with Locale.setDefault(Locale.FRENCH);

    If that's too invasive for your app, then just code your own formatter factory, which returns the formatter with the correct locale (they can even be static singletons, recall that the Jodatime formatters are inmmutable and thread safe). For example:

    public static class MyFormattterFactory {
        public static final Locale MYLOCALE = Locale.FRENCH;
    
        public static DateTimeFormatter forPattern(String p) {
            return DateTimeFormat.forPattern(p).withLocale(MYLOCALE);
        }
        // ... other similar methods
    }