Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions CodenameOne/src/com/codename1/l10n/SimpleDateFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,35 @@ public class SimpleDateFormat extends DateFormat {
*/
private List<String> patternTokens;

/**
* When set to {@code true}, month names provided by the underlying platform
* will be truncated to match the number of pattern characters. This guards
* against platforms that may return longer month names than expected for
* patterns such as {@code MMM}. The flag is {@code false} by default for
* backwards compatibility.
*/
private static boolean restrictMonthNameLength;

/**
* Enables or disables truncating of localized month names so that they
* match the number of pattern characters that triggered their lookup.
*
* @param restrict {@code true} to truncate localized month names to the
* length requested by the pattern, {@code false} to keep the platform
* provided value as-is (default behaviour).
*/
public static void setRestrictMonthNameLength(boolean restrict) {
restrictMonthNameLength = restrict;
}

/**
* @return {@code true} if localized month names should be truncated to the
* requested pattern length.
*/
public static boolean isRestrictMonthNameLength() {
return restrictMonthNameLength;
}

/**
* Construct a SimpleDateFormat with no pattern.
*/
Expand Down Expand Up @@ -348,9 +377,9 @@ String format(Date source, StringBuilder toAppendTo) {
case MONTH_LETTER:
v = calendar.get(Calendar.MONTH) - Calendar.JANUARY;
if (len > 3) {
toAppendTo.append(L10NManager.getInstance().getLongMonthName(source));
toAppendTo.append(applyMonthLengthRestriction(L10NManager.getInstance().getLongMonthName(source), len));
} else if (len == 3) {
toAppendTo.append(L10NManager.getInstance().getShortMonthName(source));
toAppendTo.append(applyMonthLengthRestriction(L10NManager.getInstance().getShortMonthName(source), len));
} else {
toAppendTo.append(leftPad(v + 1, len));
}
Expand Down Expand Up @@ -413,6 +442,13 @@ String format(Date source, StringBuilder toAppendTo) {
return toAppendTo.toString();
}

private static String applyMonthLengthRestriction(String value, int len) {
if (restrictMonthNameLength && value != null && value.length() > len) {
return value.substring(0, len);
}
return value;
}

private String[] getTimeZoneDisplayNames(String id) {
for (String zoneStrings[] : getDateFormatSymbols().getZoneStrings()) {
if (zoneStrings[DateFormatSymbols.ZONE_ID].equalsIgnoreCase(id)) {
Expand Down
Loading