summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVictor Chang <vichang@google.com>2021-01-19 10:14:57 +0000
committerVictor Chang <vichang@google.com>2024-04-19 12:13:10 +0100
commitefd9a61d83c59e0c9bc36da3defe4de6cfe126ef (patch)
tree7cd81d263116964ba30e141d0b7f25afa858c012
parentae6c41140e420c529a5bf75fe127cac1f255a0fd (diff)
downloadicu-efd9a61d83c59e0c9bc36da3defe4de6cfe126ef.tar.gz
Android patch: Add Calendar.getDateTimeFormatString used by java.time via libcore bridge
Add Calendar.getDateTimeFormatString needed by java.time. It tends to expose functionality that most end user code will not need, but allows access to ICU internal data or functionality that should be shared between ICU and java.time. - Add method to Calendar to get localized pattern string. This change was introduced in Android for the O release: https://android.googlesource.com/platform/external/icu/+/22b47ef It was then amended to remove DateFormatSymbols(ULocale, String) constructor in ICU 69, because no one is using it. https://r.android.com/q/I1d65bca1c301b5e13e265062c9eeab1196edb516 The implementation of Calendar.getDateTimeFormatString() is updated to match the new upstream internal changes. https://r.android.com/2292158 Test: n/a Change-Id: I653bb93849400cb26d0ea1f0878fc87159c79546
-rw-r--r--icu4j/main/core/src/main/java/com/ibm/icu/util/Calendar.java51
1 files changed, 50 insertions, 1 deletions
diff --git a/icu4j/main/core/src/main/java/com/ibm/icu/util/Calendar.java b/icu4j/main/core/src/main/java/com/ibm/icu/util/Calendar.java
index 926696cba..9b4de4b76 100644
--- a/icu4j/main/core/src/main/java/com/ibm/icu/util/Calendar.java
+++ b/icu4j/main/core/src/main/java/com/ibm/icu/util/Calendar.java
@@ -3755,6 +3755,51 @@ public abstract class Calendar implements Serializable, Cloneable, Comparable<Ca
return result;
}
+ // Android patch (http://b/28832222) start.
+ // Expose method to get format string for java.time.
+ /**
+ * Get the date time format string for the specified values.
+ * This is a copy of {@link #formatHelper(Calendar, ULocale, int, int)} with the following
+ * changes:
+ * <ul>
+ * <li>Made public, but hidden</li>
+ * <li>take calendar type string instead of Calendar</li>
+ * <li>Ignore overrides</li>
+ * <li>Return format string instead of DateFormat.</li>
+ * </ul>
+ * This is not meant as public API.
+ * @internal
+ */
+ // TODO: Check if calType can be passed via keyword on loc parameter instead.
+ public static String getDateTimeFormatString(ULocale loc, String calType, int dateStyle,
+ int timeStyle) {
+ if (timeStyle < DateFormat.NONE || timeStyle > DateFormat.SHORT) {
+ throw new IllegalArgumentException("Illegal time style " + timeStyle);
+ }
+ if (dateStyle < DateFormat.NONE || dateStyle > DateFormat.SHORT) {
+ throw new IllegalArgumentException("Illegal date style " + dateStyle);
+ }
+
+ PatternData patternData = PatternData.make(loc, calType);
+
+ // Resolve a pattern for the date/time style
+ String pattern = null;
+ if ((timeStyle >= 0) && (dateStyle >= 0)) {
+ pattern = SimpleFormatterImpl.formatRawPattern(
+ patternData.getDateAtTimePattern(dateStyle), 2, 2,
+ patternData.patterns[timeStyle],
+ patternData.patterns[dateStyle + 4]);
+ } else if (timeStyle >= 0) {
+ pattern = patternData.patterns[timeStyle];
+ } else if (dateStyle >= 0) {
+ pattern = patternData.patterns[dateStyle + 4];
+ } else {
+ throw new IllegalArgumentException("No date or time style specified");
+ }
+ return pattern;
+ }
+ // Android patch (http://b/28832222) end.
+
static class PatternData {
// TODO make this even more object oriented
private String[] patterns;
@@ -3782,8 +3827,12 @@ public abstract class Calendar implements Serializable, Cloneable, Comparable<Ca
}
}
private static PatternData make(Calendar cal, ULocale loc) {
+ // Android patch (http://b/28832222) start.
+ return make(loc, cal.getType());
+ }
+ private static PatternData make(ULocale loc, String calType) {
+ // Android patch (http://b/28832222) end.
// First, try to get a pattern from PATTERN_CACHE
- String calType = cal.getType();
String key = loc.getBaseName() + "+" + calType;
PatternData patternData = null;
boolean hasHourCycleKeywords = loc.getKeywordValue("rg") != null