From b140a356845c5756831414309f2692e599afcb5b Mon Sep 17 00:00:00 2001 From: Masayoshi Okutsu Date: Thu, 15 Dec 2016 13:08:01 +0900 Subject: [PATCH] 8054214: JapaneseEra.getDisplayName doesn't return names if it's an additional era Reviewed-by: rriggs, naoto --- .../classes/java/time/chrono/JapaneseEra.java | 40 +++++++++++++++---- .../Calendar/SupplementalJapaneseEraTest.java | 25 +++++++++++- .../Calendar/SupplementalJapaneseEraTest.sh | 4 +- 3 files changed, 58 insertions(+), 11 deletions(-) diff --git a/jdk/src/java.base/share/classes/java/time/chrono/JapaneseEra.java b/jdk/src/java.base/share/classes/java/time/chrono/JapaneseEra.java index f289fdb1a93..7ae62944025 100644 --- a/jdk/src/java.base/share/classes/java/time/chrono/JapaneseEra.java +++ b/jdk/src/java.base/share/classes/java/time/chrono/JapaneseEra.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2016, 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 @@ -73,11 +73,13 @@ import java.io.ObjectStreamException; import java.io.Serializable; import java.time.DateTimeException; import java.time.LocalDate; +import java.time.format.TextStyle; import java.time.temporal.ChronoField; import java.time.temporal.TemporalField; import java.time.temporal.UnsupportedTemporalTypeException; import java.time.temporal.ValueRange; import java.util.Arrays; +import java.util.Locale; import java.util.Objects; import sun.util.calendar.CalendarDate; @@ -125,8 +127,8 @@ public final class JapaneseEra */ public static final JapaneseEra HEISEI = new JapaneseEra(2, LocalDate.of(1989, 1, 8)); - // the number of defined JapaneseEra constants. - // There could be an extra era defined in its configuration. + // The number of predefined JapaneseEra constants. + // There may be a supplemental era defined by the property. private static final int N_ERA_CONSTANTS = HEISEI.getValue() + ERA_OFFSET; /** @@ -237,6 +239,32 @@ public final class JapaneseEra return Arrays.copyOf(KNOWN_ERAS, KNOWN_ERAS.length); } + /** + * Gets the textual representation of this era. + *

+ * This returns the textual name used to identify the era, + * suitable for presentation to the user. + * The parameters control the style of the returned text and the locale. + *

+ * If no textual mapping is found then the {@link #getValue() numeric value} + * is returned. + * + * @param style the style of the text required, not null + * @param locale the locale to use, not null + * @return the text value of the era, not null + * @since 9 + */ + @Override + public String getDisplayName(TextStyle style, Locale locale) { + // If this JapaneseEra is a supplemental one, obtain the name from + // the era definition. + if (getValue() > N_ERA_CONSTANTS - ERA_OFFSET) { + Objects.requireNonNull(locale, "locale"); + return style.asNormal() == TextStyle.NARROW ? getAbbreviation() : getName(); + } + return Era.super.getDisplayName(style, locale); + } + //----------------------------------------------------------------------- /** * Obtains an instance of {@code JapaneseEra} from a date. @@ -338,11 +366,7 @@ public final class JapaneseEra //----------------------------------------------------------------------- String getAbbreviation() { - int index = ordinal(getValue()); - if (index == 0) { - return ""; - } - return ERA_CONFIG[index].getAbbreviation(); + return ERA_CONFIG[ordinal(getValue())].getAbbreviation(); } String getName() { diff --git a/jdk/test/java/util/Calendar/SupplementalJapaneseEraTest.java b/jdk/test/java/util/Calendar/SupplementalJapaneseEraTest.java index 5cc0bdc67bc..b8071b05753 100644 --- a/jdk/test/java/util/Calendar/SupplementalJapaneseEraTest.java +++ b/jdk/test/java/util/Calendar/SupplementalJapaneseEraTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2014, 2016, 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 @@ -23,6 +23,8 @@ import java.text.SimpleDateFormat; import java.time.chrono.JapaneseDate; +import java.time.chrono.JapaneseEra; +import java.time.format.TextStyle; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; @@ -140,6 +142,27 @@ public class SupplementalJapaneseEraTest { System.err.printf("JapaneseDate: got=\"%s\", expected=\"%s\"%n", got, expected); errors++; } + JapaneseEra jera = jdate.getEra(); + got = jera.getDisplayName(TextStyle.FULL, Locale.US); + if (!NEW_ERA_NAME.equals(got)) { + System.err.printf("JapaneseEra (FULL): got=\"%s\", expected=\"%s\"%n", got, NEW_ERA_NAME); + errors++; + } + got = jera.getDisplayName(TextStyle.SHORT, Locale.US); + if (!NEW_ERA_NAME.equals(got)) { + System.err.printf("JapaneseEra (SHORT): got=\"%s\", expected=\"%s\"%n", got, NEW_ERA_NAME); + errors++; + } + got = jera.getDisplayName(TextStyle.NARROW, Locale.US); + if (!NEW_ERA_ABBR.equals(got)) { + System.err.printf("JapaneseEra (NARROW): got=\"%s\", expected=\"%s\"%n", got, NEW_ERA_ABBR); + errors++; + } + got = jera.getDisplayName(TextStyle.NARROW_STANDALONE, Locale.US); + if (!NEW_ERA_ABBR.equals(got)) { + System.err.printf("JapaneseEra (NARROW_STANDALONE): got=\"%s\", expected=\"%s\"%n", got, NEW_ERA_ABBR); + errors++; + } } private static void testValidation(String eraName) { diff --git a/jdk/test/java/util/Calendar/SupplementalJapaneseEraTest.sh b/jdk/test/java/util/Calendar/SupplementalJapaneseEraTest.sh index ade093c734b..5feb37d2e36 100644 --- a/jdk/test/java/util/Calendar/SupplementalJapaneseEraTest.sh +++ b/jdk/test/java/util/Calendar/SupplementalJapaneseEraTest.sh @@ -1,5 +1,5 @@ # -# Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. +# Copyright (c) 2014, 2016, 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 +# @bug 8048123 8054214 # @summary Test for jdk.calendar.japanese.supplemental.era support # @build SupplementalJapaneseEraTest # @run shell SupplementalJapaneseEraTest.sh