8173423: Wrong display name for supplemental Japanese era
authornaoto
Tue, 31 Jan 2017 13:18:34 -0800
changeset 43338 f9c4f82a8265
parent 43337 e107e6921357
child 43339 0d9c9ef833e3
8173423: Wrong display name for supplemental Japanese era Reviewed-by: sherman
jdk/src/java.base/share/classes/sun/util/locale/provider/CalendarNameProviderImpl.java
jdk/test/java/util/Calendar/SupplementalJapaneseEraTest.java
jdk/test/java/util/Calendar/SupplementalJapaneseEraTest.sh
--- a/jdk/src/java.base/share/classes/sun/util/locale/provider/CalendarNameProviderImpl.java	Mon Jan 30 14:38:08 2017 -0800
+++ b/jdk/src/java.base/share/classes/sun/util/locale/provider/CalendarNameProviderImpl.java	Tue Jan 31 13:18:34 2017 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -31,6 +31,8 @@
 import java.util.Set;
 import java.util.TreeMap;
 import java.util.spi.CalendarNameProvider;
+import sun.util.calendar.CalendarSystem;
+import sun.util.calendar.Era;
 
 /**
  * Concrete implementation of the  {@link java.util.spi.CalendarDataProvider
@@ -75,7 +77,21 @@
                 if (field == DAY_OF_WEEK || field == YEAR) {
                     --value;
                 }
-                if (value < 0 || value >= strings.length) {
+                if (value < 0 || value > strings.length) {
+                    return null;
+                } else if (value == strings.length) {
+                    if (field == ERA && "japanese".equals(calendarType)) {
+                        // get the supplemental era, if any, specified through
+                        // the property "jdk.calendar.japanese.supplemental.era"
+                        // which is always the last element.
+                        Era[] jeras = CalendarSystem.forName("japanese").getEras();
+                        if (jeras.length == value) {
+                            Era supEra = jeras[value - 1]; // 0-based index
+                            return style == LONG ?
+                                supEra.getName() :
+                                supEra.getAbbreviation();
+                        }
+                    }
                     return null;
                 }
                 name = strings[value];
--- a/jdk/test/java/util/Calendar/SupplementalJapaneseEraTest.java	Mon Jan 30 14:38:08 2017 -0800
+++ b/jdk/test/java/util/Calendar/SupplementalJapaneseEraTest.java	Tue Jan 31 13:18:34 2017 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -22,8 +22,10 @@
  */
 
 import java.text.SimpleDateFormat;
+import java.time.chrono.JapaneseChronology;
 import java.time.chrono.JapaneseDate;
 import java.time.chrono.JapaneseEra;
+import java.time.format.DateTimeFormatterBuilder;
 import java.time.format.TextStyle;
 import java.util.Calendar;
 import java.util.Date;
@@ -122,7 +124,7 @@
         got = sdf.format(firstDayOfEra);
         expected = NEW_ERA_ABBR+" 1-02-11";
         if (!expected.equals(got)) {
-            System.err.printf("GGGG y-MM-dd: got=\"%s\", expected=\"%s\"%n", got, expected);
+            System.err.printf("G y-MM-dd: got=\"%s\", expected=\"%s\"%n", got, expected);
             errors++;
         }
 
@@ -163,6 +165,20 @@
             System.err.printf("JapaneseEra (NARROW_STANDALONE): got=\"%s\", expected=\"%s\"%n", got, NEW_ERA_ABBR);
             errors++;
         }
+
+        // test long/abbreviated names with java.time.format
+        got = new DateTimeFormatterBuilder()
+            .appendPattern("GGGG")
+            .appendLiteral(" ")
+            .appendPattern("G")
+            .toFormatter(Locale.US)
+            .withChronology(JapaneseChronology.INSTANCE)
+            .format(jdate);
+        expected = NEW_ERA_NAME + " " + NEW_ERA_ABBR;
+        if (!expected.equals(got)) {
+            System.err.printf("java.time formatter long/abbr names: got=\"%s\", expected=\"%s\"%n", got, expected);
+            errors++;
+        }
     }
 
     private static void testValidation(String eraName) {
--- a/jdk/test/java/util/Calendar/SupplementalJapaneseEraTest.sh	Mon Jan 30 14:38:08 2017 -0800
+++ b/jdk/test/java/util/Calendar/SupplementalJapaneseEraTest.sh	Tue Jan 31 13:18:34 2017 -0800
@@ -1,5 +1,5 @@
 #
-# Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
+# Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 #
 # This code is free software; you can redistribute it and/or modify it
@@ -22,7 +22,7 @@
 #
 
 # @test
-# @bug 8048123 8054214
+# @bug 8048123 8054214 8173423
 # @summary Test for jdk.calendar.japanese.supplemental.era support
 # @build SupplementalJapaneseEraTest
 # @run shell SupplementalJapaneseEraTest.sh