Skip to content

Commit 3a7b30e

Browse files
authored
Add optional month name length restriction (#3968)
1 parent b304b60 commit 3a7b30e

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

CodenameOne/src/com/codename1/l10n/SimpleDateFormat.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,35 @@ public class SimpleDateFormat extends DateFormat {
175175
*/
176176
private List<String> patternTokens;
177177

178+
/**
179+
* When set to {@code true}, month names provided by the underlying platform
180+
* will be truncated to match the number of pattern characters. This guards
181+
* against platforms that may return longer month names than expected for
182+
* patterns such as {@code MMM}. The flag is {@code false} by default for
183+
* backwards compatibility.
184+
*/
185+
private static boolean restrictMonthNameLength;
186+
187+
/**
188+
* Enables or disables truncating of localized month names so that they
189+
* match the number of pattern characters that triggered their lookup.
190+
*
191+
* @param restrict {@code true} to truncate localized month names to the
192+
* length requested by the pattern, {@code false} to keep the platform
193+
* provided value as-is (default behaviour).
194+
*/
195+
public static void setRestrictMonthNameLength(boolean restrict) {
196+
restrictMonthNameLength = restrict;
197+
}
198+
199+
/**
200+
* @return {@code true} if localized month names should be truncated to the
201+
* requested pattern length.
202+
*/
203+
public static boolean isRestrictMonthNameLength() {
204+
return restrictMonthNameLength;
205+
}
206+
178207
/**
179208
* Construct a SimpleDateFormat with no pattern.
180209
*/
@@ -348,9 +377,9 @@ String format(Date source, StringBuilder toAppendTo) {
348377
case MONTH_LETTER:
349378
v = calendar.get(Calendar.MONTH) - Calendar.JANUARY;
350379
if (len > 3) {
351-
toAppendTo.append(L10NManager.getInstance().getLongMonthName(source));
380+
toAppendTo.append(applyMonthLengthRestriction(L10NManager.getInstance().getLongMonthName(source), len));
352381
} else if (len == 3) {
353-
toAppendTo.append(L10NManager.getInstance().getShortMonthName(source));
382+
toAppendTo.append(applyMonthLengthRestriction(L10NManager.getInstance().getShortMonthName(source), len));
354383
} else {
355384
toAppendTo.append(leftPad(v + 1, len));
356385
}
@@ -413,6 +442,13 @@ String format(Date source, StringBuilder toAppendTo) {
413442
return toAppendTo.toString();
414443
}
415444

445+
private static String applyMonthLengthRestriction(String value, int len) {
446+
if (restrictMonthNameLength && value != null && value.length() > len) {
447+
return value.substring(0, len);
448+
}
449+
return value;
450+
}
451+
416452
private String[] getTimeZoneDisplayNames(String id) {
417453
for (String zoneStrings[] : getDateFormatSymbols().getZoneStrings()) {
418454
if (zoneStrings[DateFormatSymbols.ZONE_ID].equalsIgnoreCase(id)) {

0 commit comments

Comments
 (0)