ICU-20629 DTPG: Fixing uncaught exception in Java.
diff --git a/icu4c/source/test/intltest/dtptngts.cpp b/icu4c/source/test/intltest/dtptngts.cpp
index 4a55bb7..3fdafe4 100644
--- a/icu4c/source/test/intltest/dtptngts.cpp
+++ b/icu4c/source/test/intltest/dtptngts.cpp
@@ -1416,6 +1416,11 @@
{"ars", u"h a", u"h:mm a"},
// en_NH is interesting because NH is a depregated region code.
{"en_NH", u"h a", u"h:mm a"},
+ // ch_ZH is a typo (should be zh_CN), but we should fail gracefully.
+ // {"cn_ZH", u"HH", u"H:mm"}, // TODO(ICU-20653): Desired behavior
+ {"cn_ZH", u"HH", u"h:mm a"}, // Actual behavior
+ // a non-BCP47 locale without a country code should not fail
+ {"ja_TRADITIONAL", u"H時", u"H:mm"},
};
for (auto& cas : cases) {
diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/DateTimePatternGenerator.java b/icu4j/main/classes/core/src/com/ibm/icu/text/DateTimePatternGenerator.java
index 81e2fce..a730503 100644
--- a/icu4j/main/classes/core/src/com/ibm/icu/text/DateTimePatternGenerator.java
+++ b/icu4j/main/classes/core/src/com/ibm/icu/text/DateTimePatternGenerator.java
@@ -351,6 +351,8 @@
String language = uLocale.getLanguage();
String country = uLocale.getCountry();
if (language.isEmpty() || country.isEmpty()) {
+ // Note: addLikelySubtags is documented not to throw in Java,
+ // unlike in C++.
ULocale max = ULocale.addLikelySubtags(uLocale);
language = max.getLanguage();
country = max.getCountry();
@@ -368,9 +370,13 @@
// Check if the region has an alias
if (list == null) {
- Region region = Region.getInstance(country);
- country = region.toString();
- list = getAllowedHourFormatsLangCountry(language, country);
+ try {
+ Region region = Region.getInstance(country);
+ country = region.toString();
+ list = getAllowedHourFormatsLangCountry(language, country);
+ } catch (IllegalArgumentException e) {
+ // invalid region; fall through
+ }
}
if (list != null) {
diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/DateTimeGeneratorTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/DateTimeGeneratorTest.java
index b5874dc..767bb60 100644
--- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/DateTimeGeneratorTest.java
+++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/DateTimeGeneratorTest.java
@@ -1736,6 +1736,11 @@
{"ars", "h a", "h:mm a"},
// en_NH is interesting because NH is a depregated region code.
{"en_NH", "h a", "h:mm a"},
+ // ch_ZH is a typo (should be zh_CN), but we should fail gracefully.
+ // {"cn_ZH", "HH", "H:mm"}, // TODO(ICU-20653): Desired behavior
+ {"cn_ZH", "HH", "h:mm a"}, // Actual behavior
+ // a non-BCP47 locale without a country code should not fail
+ {"ja_TRADITIONAL", "H時", "H:mm"},
};
for (String[] cas : cases) {