下面是我的代码,它揭示了一个Joda时间错误:
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
}
}根据JavaDocs,我应该得到英文格式的句号。但它使用的是当前的默认区域设置,在上面的示例中是德语。
我在MacOSX10.7上使用Joda Time 2.0,电脑的首选语言是“澳大利亚英语”。
有什么简单的变通方法可以推荐吗?
发布于 2012-04-16 19:08:54
另一个解决方案是更新到此错误报告的最新Joda Time source...according,现在已修复:http://sourceforge.net/tracker/?func=detail&aid=3471414&group_id=97367&atid=617889
发布于 2012-04-11 20:58:02
好的,我找到了一个变通方法:
final PeriodFormatter periodFormatter =
PeriodFormat.wordBased(new Locale(""));然而,PeriodFormat中显然存在一个bug。例如,PeriodFormat.getDefault()的JavaDocs表示:
Gets the default formatter that outputs words in English.
This calls {@link #wordBased(Locale)} using a locale of {@code ENGLISH}.而我的结果是,当我执行以下命令时,将使用用户的当前区域设置:
final PeriodFormatter periodFormatter = PeriodFormat.getDefault();https://stackoverflow.com/questions/10106143
复制相似问题