Skip to content

Commit 9982995

Browse files
committed
8358819: The first year is not displayed correctly in Japanese Calendar
Reviewed-by: jlu, joehw, iris
1 parent 0ef0986 commit 9982995

File tree

3 files changed

+131
-77
lines changed

3 files changed

+131
-77
lines changed

make/jdk/src/classes/build/tools/cldrconverter/Bundle.java

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -542,10 +542,10 @@ private void handleDateTimeFormatPatterns(String[] patternKeys, Map<String, Obje
542542
if (pattern != null) {
543543
// Perform date-time format pattern conversion which is
544544
// applicable to both SimpleDateFormat and j.t.f.DateTimeFormatter.
545-
String transPattern = translateDateFormatLetters(calendarType, pattern, this::convertDateTimePatternLetter);
545+
String transPattern = translateDateFormatLetters(calendarType, key, pattern, this::convertDateTimePatternLetter);
546546
dateTimePatterns.add(i, transPattern);
547547
// Additionally, perform SDF specific date-time format pattern conversion
548-
sdfPatterns.add(i, translateDateFormatLetters(calendarType, transPattern, this::convertSDFLetter));
548+
sdfPatterns.add(i, translateDateFormatLetters(calendarType, key, transPattern, this::convertSDFLetter));
549549
} else {
550550
dateTimePatterns.add(i, null);
551551
sdfPatterns.add(i, null);
@@ -568,7 +568,7 @@ private void handleDateTimeFormatPatterns(String[] patternKeys, Map<String, Obje
568568
}
569569
}
570570

571-
private String translateDateFormatLetters(CalendarType calendarType, String cldrFormat, ConvertDateTimeLetters converter) {
571+
private String translateDateFormatLetters(CalendarType calendarType, String patternKey, String cldrFormat, ConvertDateTimeLetters converter) {
572572
String pattern = cldrFormat;
573573
int length = pattern.length();
574574
boolean inQuote = false;
@@ -587,7 +587,7 @@ private String translateDateFormatLetters(CalendarType calendarType, String cldr
587587
if (nextc == '\'') {
588588
i++;
589589
if (count != 0) {
590-
converter.convert(calendarType, lastLetter, count, jrePattern);
590+
converter.convert(calendarType, patternKey, lastLetter, count, jrePattern);
591591
lastLetter = 0;
592592
count = 0;
593593
}
@@ -597,7 +597,7 @@ private String translateDateFormatLetters(CalendarType calendarType, String cldr
597597
}
598598
if (!inQuote) {
599599
if (count != 0) {
600-
converter.convert(calendarType, lastLetter, count, jrePattern);
600+
converter.convert(calendarType, patternKey, lastLetter, count, jrePattern);
601601
lastLetter = 0;
602602
count = 0;
603603
}
@@ -614,7 +614,7 @@ private String translateDateFormatLetters(CalendarType calendarType, String cldr
614614
}
615615
if (!(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')) {
616616
if (count != 0) {
617-
converter.convert(calendarType, lastLetter, count, jrePattern);
617+
converter.convert(calendarType, patternKey, lastLetter, count, jrePattern);
618618
lastLetter = 0;
619619
count = 0;
620620
}
@@ -627,7 +627,7 @@ private String translateDateFormatLetters(CalendarType calendarType, String cldr
627627
count++;
628628
continue;
629629
}
630-
converter.convert(calendarType, lastLetter, count, jrePattern);
630+
converter.convert(calendarType, patternKey, lastLetter, count, jrePattern);
631631
lastLetter = c;
632632
count = 1;
633633
}
@@ -637,7 +637,7 @@ private String translateDateFormatLetters(CalendarType calendarType, String cldr
637637
}
638638

639639
if (count != 0) {
640-
converter.convert(calendarType, lastLetter, count, jrePattern);
640+
converter.convert(calendarType, patternKey, lastLetter, count, jrePattern);
641641
}
642642
if (cldrFormat.contentEquals(jrePattern)) {
643643
return cldrFormat;
@@ -661,7 +661,7 @@ private String toMetaZoneKey(String tzKey) {
661661
* on the support given by the SimpleDateFormat and the j.t.f.DateTimeFormatter
662662
* for date-time formatting.
663663
*/
664-
private void convertDateTimePatternLetter(CalendarType calendarType, char cldrLetter, int count, StringBuilder sb) {
664+
private void convertDateTimePatternLetter(CalendarType calendarType, String patternKey, char cldrLetter, int count, StringBuilder sb) {
665665
switch (cldrLetter) {
666666
case 'u':
667667
case 'U':
@@ -683,7 +683,7 @@ private void convertDateTimePatternLetter(CalendarType calendarType, char cldrLe
683683
* Perform a conversion of CLDR date-time format pattern letter which is
684684
* specific to the SimpleDateFormat.
685685
*/
686-
private void convertSDFLetter(CalendarType calendarType, char cldrLetter, int count, StringBuilder sb) {
686+
private void convertSDFLetter(CalendarType calendarType, String patternKey, char cldrLetter, int count, StringBuilder sb) {
687687
switch (cldrLetter) {
688688
case 'G':
689689
if (calendarType != CalendarType.GREGORIAN) {
@@ -722,6 +722,17 @@ private void convertSDFLetter(CalendarType calendarType, char cldrLetter, int co
722722
appendN('z', count, sb);
723723
break;
724724

725+
case 'y':
726+
// If the style is FULL/LONG for a Japanese Calendar, make the
727+
// count == 4 for Gan-nen
728+
if (calendarType == CalendarType.JAPANESE &&
729+
(patternKey.contains("full-") ||
730+
patternKey.contains("long-"))) {
731+
count = 4;
732+
}
733+
appendN(cldrLetter, count, sb);
734+
break;
735+
725736
case 'Z':
726737
if (count == 4 || count == 5) {
727738
sb.append("XXX");
@@ -767,6 +778,7 @@ private void handleSkeletonPatterns(Map<String, Object> myMap, CalendarType cale
767778
.collect(Collectors.toMap(
768779
e -> calendarPrefix + e.getKey(),
769780
e -> translateDateFormatLetters(calendarType,
781+
e.getKey(),
770782
(String)e.getValue(),
771783
this::convertDateTimePatternLetter)
772784
))
@@ -775,7 +787,7 @@ private void handleSkeletonPatterns(Map<String, Object> myMap, CalendarType cale
775787

776788
@FunctionalInterface
777789
private interface ConvertDateTimeLetters {
778-
void convert(CalendarType calendarType, char cldrLetter, int count, StringBuilder sb);
790+
void convert(CalendarType calendarType, String patternKey, char cldrLetter, int count, StringBuilder sb);
779791
}
780792

781793
/**
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8202088 8207152 8217609 8219890 8358819
27+
* @summary Test the localized Japanese calendar names, such as
28+
* the Reiwa Era names (May 1st. 2019-), or the Gan-nen text
29+
* @modules jdk.localedata
30+
* @run junit JapaneseCalendarNameTest
31+
*/
32+
33+
import static java.util.Calendar.*;
34+
import static java.util.Locale.*;
35+
36+
import java.text.DateFormat;
37+
import java.text.ParseException;
38+
import java.util.Calendar;
39+
import java.util.Locale;
40+
import java.util.stream.Stream;
41+
42+
import org.junit.jupiter.params.ParameterizedTest;
43+
import org.junit.jupiter.params.provider.Arguments;
44+
import org.junit.jupiter.params.provider.MethodSource;
45+
46+
import static org.junit.jupiter.api.Assertions.assertEquals;
47+
48+
public class JapaneseCalendarNameTest {
49+
private static final Calendar c = new Calendar.Builder()
50+
.setCalendarType("japanese")
51+
.setFields(ERA, 5, YEAR, 1, MONTH, MAY, DAY_OF_MONTH, 1)
52+
.build();
53+
private static final Locale JAJPJP = Locale.of("ja", "JP", "JP");
54+
private static final Locale JCAL = Locale.forLanguageTag("ja-u-ca-japanese");
55+
56+
private static Stream<Arguments> reiwaEraNames() {
57+
return Stream.of(
58+
// type, locale, name
59+
Arguments.of(LONG, JAPAN, "令和"),
60+
Arguments.of(LONG, US, "Reiwa"),
61+
Arguments.of(LONG, CHINA, "令和"),
62+
Arguments.of(SHORT, JAPAN, "令和"),
63+
Arguments.of(SHORT, US, "Reiwa"),
64+
Arguments.of(SHORT, CHINA, "令和")
65+
);
66+
}
67+
68+
@ParameterizedTest
69+
@MethodSource("reiwaEraNames")
70+
void testReiwaEraName(int type, Locale locale, String expected) {
71+
assertEquals(expected, c.getDisplayName(ERA, type, locale));
72+
}
73+
74+
private static Stream<Arguments> gannen() {
75+
return Stream.of(
76+
// format,
77+
// formatted text
78+
Arguments.of(DateFormat.getDateInstance(DateFormat.FULL, JAJPJP),
79+
"令和元年5月1日水曜日"),
80+
Arguments.of(DateFormat.getDateInstance(DateFormat.FULL, JCAL),
81+
"令和元年5月1日水曜日"),
82+
Arguments.of(DateFormat.getDateInstance(DateFormat.LONG, JAJPJP),
83+
"令和元年5月1日"),
84+
Arguments.of(DateFormat.getDateInstance(DateFormat.LONG, JCAL),
85+
"令和元年5月1日"),
86+
Arguments.of(DateFormat.getDateInstance(DateFormat.MEDIUM, JAJPJP),
87+
"令和1年5月1日"),
88+
Arguments.of(DateFormat.getDateInstance(DateFormat.MEDIUM, JCAL),
89+
"令和1年5月1日"),
90+
Arguments.of(DateFormat.getDateInstance(DateFormat.SHORT, JAJPJP),
91+
"令和1/5/1"),
92+
Arguments.of(DateFormat.getDateInstance(DateFormat.SHORT, JCAL),
93+
"令和1/5/1")
94+
);
95+
}
96+
97+
@ParameterizedTest
98+
@MethodSource("gannen")
99+
void testGannenFormat(DateFormat df, String expected) {
100+
assertEquals(expected, df.format(c.getTime()));
101+
}
102+
103+
@ParameterizedTest
104+
@MethodSource("gannen")
105+
void testGannenParse(DateFormat df, String formatted) throws ParseException {
106+
assertEquals(c.getTime(), df.parse(formatted));
107+
}
108+
}

test/jdk/java/util/Calendar/JapaneseEraNameTest.java

Lines changed: 0 additions & 66 deletions
This file was deleted.

0 commit comments

Comments
 (0)