Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,9 @@ private static final class WeekYear implements NumberRule {

@Override
public void appendTo(final Appendable buffer, final Calendar calendar) throws IOException {
rule.appendTo(buffer, calendar.getWeekYear());
// Some Calendar implementations (JapaneseImperialCalendar) do not support week-dates.
// Fall back to Calendar.YEAR in that case.
rule.appendTo(buffer, calendar.isWeekDateSupported() ? calendar.getWeekYear() : calendar.get(Calendar.YEAR));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,36 @@ private AtomicLongArray measureTime(final Format printer, final Format parser) t
return totalElapsed;
}

/**
* Pre-patch: UnsupportedOperationException when formatting a Japanese Imperial Calendar with 'Y' (week-year) pattern.
* <p>
* Post-patch: falls back to Calendar.YEAR and formats successfully.
* </p>
*/
@Test
void testJapaneseImperialCalendarWeekYearDoesNotThrow() {
final Locale japaneseImperial = new Locale("ja", "JP", "JP");
final Calendar japaneseCal = Calendar.getInstance(japaneseImperial);
final FastDateFormat fdp = FastDateFormat.getInstance("YYYY-MM-dd");
assertNotNull(fdp.format(japaneseCal), "Formatting JapaneseImperialCalendar with YYYY pattern must not throw UnsupportedOperationException");
}

/**
* Pre-patch: UnsupportedOperationException when formatting a Japanese Imperial
* <p>
* Calendar with 'Y' (week-year) pattern. Post-patch: falls back to Calendar.YEAR and formats successfully.
* </p>
* <p>
* Also test that a regular Gregorian calendar works fine with YYYY.
* </p>
*/
@Test
void testGregorianCalendarWeekYearWorks() {
final Calendar cal = Calendar.getInstance();
final FastDateFormat fdp = FastDateFormat.getInstance("YYYY-MM-dd");
assertNotNull(fdp.format(cal), "Formatting Gregorian Calendar with YYYY pattern should always work");
}

@DefaultLocale(language = "en", country = "US")
@Test
void test_changeDefault_Locale_DateInstance() {
Expand Down
Loading