mirror of
https://github.com/apache/commons-lang.git
synced 2025-03-01 13:39:04 +00:00
Applying Sebb's test and fix from LANG-368 - fixing it so that FastDateFormat getDateInstance and getDateTimeInstance continue to work if Locale.getDefault() changes
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@590551 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
43cf3f491e
commit
127e6e338f
@ -281,16 +281,15 @@ public static synchronized FastDateFormat getDateInstance(int style, TimeZone ti
|
|||||||
if (timeZone != null) {
|
if (timeZone != null) {
|
||||||
key = new Pair(key, timeZone);
|
key = new Pair(key, timeZone);
|
||||||
}
|
}
|
||||||
if (locale != null) {
|
|
||||||
key = new Pair(key, locale);
|
if (locale == null) {
|
||||||
|
locale = Locale.getDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
key = new Pair(key, locale);
|
||||||
|
|
||||||
FastDateFormat format = (FastDateFormat) cDateInstanceCache.get(key);
|
FastDateFormat format = (FastDateFormat) cDateInstanceCache.get(key);
|
||||||
if (format == null) {
|
if (format == null) {
|
||||||
if (locale == null) {
|
|
||||||
locale = Locale.getDefault();
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
SimpleDateFormat formatter = (SimpleDateFormat) DateFormat.getDateInstance(style, locale);
|
SimpleDateFormat formatter = (SimpleDateFormat) DateFormat.getDateInstance(style, locale);
|
||||||
String pattern = formatter.toPattern();
|
String pattern = formatter.toPattern();
|
||||||
@ -461,16 +460,13 @@ public static synchronized FastDateFormat getDateTimeInstance(int dateStyle, int
|
|||||||
if (timeZone != null) {
|
if (timeZone != null) {
|
||||||
key = new Pair(key, timeZone);
|
key = new Pair(key, timeZone);
|
||||||
}
|
}
|
||||||
if (locale != null) {
|
if (locale == null) {
|
||||||
key = new Pair(key, locale);
|
locale = Locale.getDefault();
|
||||||
}
|
}
|
||||||
|
key = new Pair(key, locale);
|
||||||
|
|
||||||
FastDateFormat format = (FastDateFormat) cDateTimeInstanceCache.get(key);
|
FastDateFormat format = (FastDateFormat) cDateTimeInstanceCache.get(key);
|
||||||
if (format == null) {
|
if (format == null) {
|
||||||
if (locale == null) {
|
|
||||||
locale = Locale.getDefault();
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
SimpleDateFormat formatter = (SimpleDateFormat) DateFormat.getDateTimeInstance(dateStyle, timeStyle,
|
SimpleDateFormat formatter = (SimpleDateFormat) DateFormat.getDateTimeInstance(dateStyle, timeStyle,
|
||||||
locale);
|
locale);
|
||||||
|
@ -132,6 +132,46 @@ public void test_getInstance_String_Locale() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void test_changeDefault_Locale_DateInstance() {
|
||||||
|
Locale realDefaultLocale = Locale.getDefault();
|
||||||
|
try {
|
||||||
|
Locale.setDefault(Locale.US);
|
||||||
|
FastDateFormat format1 = FastDateFormat.getDateInstance(FastDateFormat.FULL, Locale.GERMANY);
|
||||||
|
FastDateFormat format2 = FastDateFormat.getDateInstance(FastDateFormat.FULL);
|
||||||
|
Locale.setDefault(Locale.GERMANY);
|
||||||
|
FastDateFormat format3 = FastDateFormat.getDateInstance(FastDateFormat.FULL);
|
||||||
|
|
||||||
|
assertSame(Locale.GERMANY, format1.getLocale());
|
||||||
|
assertSame(Locale.US, format2.getLocale());
|
||||||
|
assertSame(Locale.GERMANY, format3.getLocale());
|
||||||
|
assertTrue(format1 != format2); // -- junit 3.8 version -- assertFalse(format1 == format2);
|
||||||
|
assertTrue(format2 != format3);
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
Locale.setDefault(realDefaultLocale);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void test_changeDefault_Locale_DateTimeInstance() {
|
||||||
|
Locale realDefaultLocale = Locale.getDefault();
|
||||||
|
try {
|
||||||
|
Locale.setDefault(Locale.US);
|
||||||
|
FastDateFormat format1 = FastDateFormat.getDateTimeInstance(FastDateFormat.FULL, FastDateFormat.FULL, Locale.GERMANY);
|
||||||
|
FastDateFormat format2 = FastDateFormat.getDateTimeInstance(FastDateFormat.FULL, FastDateFormat.FULL);
|
||||||
|
Locale.setDefault(Locale.GERMANY);
|
||||||
|
FastDateFormat format3 = FastDateFormat.getDateTimeInstance(FastDateFormat.FULL, FastDateFormat.FULL);
|
||||||
|
|
||||||
|
assertSame(Locale.GERMANY, format1.getLocale());
|
||||||
|
assertSame(Locale.US, format2.getLocale());
|
||||||
|
assertSame(Locale.GERMANY, format3.getLocale());
|
||||||
|
assertTrue(format1 != format2); // -- junit 3.8 version -- assertFalse(format1 == format2);
|
||||||
|
assertTrue(format2 != format3);
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
Locale.setDefault(realDefaultLocale);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void test_getInstance_String_TimeZone_Locale() {
|
public void test_getInstance_String_TimeZone_Locale() {
|
||||||
Locale realDefaultLocale = Locale.getDefault();
|
Locale realDefaultLocale = Locale.getDefault();
|
||||||
TimeZone realDefaultZone = TimeZone.getDefault();
|
TimeZone realDefaultZone = TimeZone.getDefault();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user