3

Here's my code which reveals a Joda Time bug:

import org.joda.time.Period;
import org.joda.time.format.PeriodFormat;
import org.joda.time.format.PeriodFormatter;

import java.util.Locale;

public class ScratchSpace {

    public static void main(String[] args) {
        Locale.setDefault(Locale.GERMAN);
        final PeriodFormatter periodFormatter = 
                 PeriodFormat.wordBased(Locale.ENGLISH);
        final Period period = new Period(6, 5, 4, 3);
        final String s = period.toString(periodFormatter);
        // i'm expecting english to be outputted
        System.out.println("s = " + s); // outputs german: 6 Stunden, 5 Minuten, 4 Sekunden und 3 Millisekunden
    }

}

According to the JavaDocs I should be getting the period formatted in English. But it is using the current default locale instead, which in the example above is German.

I'm using Joda Time 2.0, on Mac OS X 10.7, with the computer set to "Australian English" as the preferred language.

Any simple work-around you can suggest?

4

2 回答 2

1

Okay, I've found a work-around:

final PeriodFormatter periodFormatter = 
             PeriodFormat.wordBased(new Locale(""));

However there is clearly a bug present in PeriodFormat. For example, the JavaDocs for PeriodFormat.getDefault() says:

Gets the default formatter that outputs words in English.
 This calls {@link #wordBased(Locale)} using a locale of {@code ENGLISH}.

whereas the result for me is that the user's current locale is used instead when I execute this:

final PeriodFormatter periodFormatter = PeriodFormat.getDefault();
于 2012-04-11T12:58:01.577 回答
1

Another solution is to update to the latest Joda Time source...according to this bug report it is now fixed: http://sourceforge.net/tracker/?func=detail&aid=3471414&group_id=97367&atid=617889

于 2012-04-16T11:08:54.197 回答