--- old/src/java.base/share/classes/java/time/format/DateTimeFormatterBuilder.java 2019-07-02 19:28:15.895465700 +0530 +++ new/src/java.base/share/classes/java/time/format/DateTimeFormatterBuilder.java 2019-07-02 19:28:14.246300800 +0530 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2019, 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 @@ -85,7 +85,6 @@ import java.time.ZoneId; import java.time.ZoneOffset; import java.time.chrono.ChronoLocalDate; -import java.time.chrono.ChronoLocalDateTime; import java.time.chrono.Chronology; import java.time.chrono.Era; import java.time.chrono.IsoChronology; @@ -122,7 +121,6 @@ import sun.text.spi.JavaTimeDateTimePatternProvider; import sun.util.locale.provider.CalendarDataUtility; import sun.util.locale.provider.LocaleProviderAdapter; -import sun.util.locale.provider.LocaleResources; import sun.util.locale.provider.TimeZoneNameUtility; /** @@ -3871,7 +3869,11 @@ if (offsetSecs == null) { return false; } - String gmtText = "GMT"; // TODO: get localized version of 'GMT' + String key = "timezone.gmtZeroFormat"; + String gmtText = DateTimeTextProvider.getLocalizedResource(key, context.getLocale()); + if(gmtText == null) { + gmtText = "GMT"; // Default to "GMT" + } buf.append(gmtText); int totalSecs = Math.toIntExact(offsetSecs); if (totalSecs != 0) { @@ -3917,7 +3919,11 @@ public int parse(DateTimeParseContext context, CharSequence text, int position) { int pos = position; int end = text.length(); - String gmtText = "GMT"; // TODO: get localized version of 'GMT' + String key = "timezone.gmtZeroFormat"; + String gmtText = DateTimeTextProvider.getLocalizedResource(key, context.getLocale()); + if(gmtText == null) { + gmtText = "GMT"; // Default to "GMT" + } if (!context.subSequenceEquals(text, pos, gmtText, 0, gmtText.length())) { return ~position; } --- old/test/jdk/java/time/tck/java/time/format/TCKOffsetPrinterParser.java 2019-07-02 19:28:27.715647600 +0530 +++ new/test/jdk/java/time/tck/java/time/format/TCKOffsetPrinterParser.java 2019-07-02 19:28:25.975473600 +0530 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2019, 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 @@ -69,6 +69,7 @@ import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; import java.time.format.TextStyle; +import java.util.Locale; import org.testng.annotations.BeforeMethod; import org.testng.annotations.DataProvider; @@ -80,6 +81,8 @@ @Test public class TCKOffsetPrinterParser { + private static final LocalDateTime DT_2012_06_30_12_30_40 = LocalDateTime.of(2012, 6, 30, 12, 30, 40); + private static final ZoneOffset OFFSET_UTC = ZoneOffset.UTC; private static final ZoneOffset OFFSET_P0100 = ZoneOffset.ofHours(1); private static final ZoneOffset OFFSET_P0123 = ZoneOffset.ofHoursMinutes(1, 23); @@ -91,7 +94,6 @@ private static final ZoneOffset OFFSET_M0023 = ZoneOffset.ofHoursMinutes(0, -23); private static final ZoneOffset OFFSET_M012345 = ZoneOffset.ofHoursMinutesSeconds(-1, -23, -45); private static final ZoneOffset OFFSET_M000045 = ZoneOffset.ofHoursMinutesSeconds(0, 0, -45); - private static final LocalDateTime DT_2012_06_30_12_30_40 = LocalDateTime.of(2012, 6, 30, 12, 30, 40); private static final ZoneOffset OFFSET_P1100 = ZoneOffset.ofHours(11); private static final ZoneOffset OFFSET_P1123 = ZoneOffset.ofHoursMinutes(11, 23); @@ -101,6 +103,9 @@ private static final ZoneOffset OFFSET_M1100 = ZoneOffset.ofHours(-11); private static final ZoneOffset OFFSET_M1123 = ZoneOffset.ofHoursMinutes(-11, -23); private static final ZoneOffset OFFSET_M112345 = ZoneOffset.ofHoursMinutesSeconds(-11, -23, -45); + + private static final Locale LOCALE_US = Locale.US; + private DateTimeFormatterBuilder builder; @BeforeMethod @@ -443,28 +448,28 @@ @DataProvider(name="print_localized") Object[][] data_print_localized() { return new Object[][] { - {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_UTC, "GMT"}, - {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P0100, "GMT+01:00"}, - {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P0123, "GMT+01:23"}, - {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P0023, "GMT+00:23"}, - {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P012345, "GMT+01:23:45"}, - {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M000045, "GMT-00:00:45"}, - {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M0100, "GMT-01:00"}, - {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M0123, "GMT-01:23"}, - {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M0023, "GMT-00:23"}, - {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M012345, "GMT-01:23:45"}, - {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M000045, "GMT-00:00:45"}, - {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_UTC, "GMT"}, - {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_P0100, "GMT+1"}, - {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_P0123, "GMT+1:23"}, - {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_P0023, "GMT+0:23"}, - {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_P012345, "GMT+1:23:45"}, - {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M000045, "GMT-0:00:45"}, - {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M0100, "GMT-1"}, - {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M0123, "GMT-1:23"}, - {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M0023, "GMT-0:23"}, - {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M012345, "GMT-1:23:45"}, - {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M000045, "GMT-0:00:45"}, + {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_UTC, LOCALE_US, "GMT"}, + {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P0100, LOCALE_US, "GMT+01:00"}, + {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P0123, LOCALE_US, "GMT+01:23"}, + {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P0023, LOCALE_US, "GMT+00:23"}, + {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P012345, LOCALE_US, "GMT+01:23:45"}, + {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M000045, LOCALE_US, "GMT-00:00:45"}, + {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M0100, LOCALE_US, "GMT-01:00"}, + {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M0123, LOCALE_US, "GMT-01:23"}, + {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M0023, LOCALE_US, "GMT-00:23"}, + {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M012345, LOCALE_US, "GMT-01:23:45"}, + {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M000045, LOCALE_US, "GMT-00:00:45"}, + {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_UTC, LOCALE_US, "GMT"}, + {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_P0100, LOCALE_US, "GMT+1"}, + {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_P0123, LOCALE_US, "GMT+1:23"}, + {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_P0023, LOCALE_US, "GMT+0:23"}, + {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_P012345, LOCALE_US, "GMT+1:23:45"}, + {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M000045, LOCALE_US, "GMT-0:00:45"}, + {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M0100, LOCALE_US, "GMT-1"}, + {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M0123, LOCALE_US, "GMT-1:23"}, + {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M0023, LOCALE_US, "GMT-0:23"}, + {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M012345, LOCALE_US, "GMT-1:23:45"}, + {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M000045, LOCALE_US, "GMT-0:00:45"} }; } @@ -529,7 +534,6 @@ @Test(dataProvider="print") public void test_print_pattern_Z(String offsetPattern, String noOffset, LocalDateTime ldt, ZoneId zone, String expected) { - String pattern = null; if (offsetPattern.equals("+HHMM") && noOffset.equals("Z")) { ZonedDateTime zdt = ldt.atZone(zone); DateTimeFormatter f1 = new DateTimeFormatterBuilder().appendPattern("Z").toFormatter(); @@ -552,37 +556,39 @@ } @Test(dataProvider="print_localized") - public void test_print_localized(TextStyle style, LocalDateTime ldt, ZoneOffset offset, String expected) { + public void test_print_localized(TextStyle style, LocalDateTime ldt, ZoneOffset offset, Locale locale, String expected) { + OffsetDateTime odt = OffsetDateTime.of(ldt, offset); ZonedDateTime zdt = ldt.atZone(offset); - - DateTimeFormatter f = new DateTimeFormatterBuilder().appendLocalizedOffset(style) - .toFormatter(); + + builder = new DateTimeFormatterBuilder().appendLocalizedOffset(style); + DateTimeFormatter f = (locale != null) ? builder.toFormatter(locale) : builder.toFormatter(); assertEquals(f.format(odt), expected); assertEquals(f.format(zdt), expected); assertEquals(f.parse(expected, ZoneOffset::from), offset); if (style == TextStyle.FULL) { - f = new DateTimeFormatterBuilder().appendPattern("ZZZZ") - .toFormatter(); + builder = new DateTimeFormatterBuilder().appendPattern("ZZZZ"); + f = (locale != null) ? builder.toFormatter(locale) : builder.toFormatter(); assertEquals(f.format(odt), expected); assertEquals(f.format(zdt), expected); assertEquals(f.parse(expected, ZoneOffset::from), offset); - f = new DateTimeFormatterBuilder().appendPattern("OOOO") - .toFormatter(); + builder = new DateTimeFormatterBuilder().appendPattern("OOOO"); + f = (locale != null) ? builder.toFormatter(locale) : builder.toFormatter(); assertEquals(f.format(odt), expected); assertEquals(f.format(zdt), expected); assertEquals(f.parse(expected, ZoneOffset::from), offset); } if (style == TextStyle.SHORT) { - f = new DateTimeFormatterBuilder().appendPattern("O") - .toFormatter(); + builder = new DateTimeFormatterBuilder().appendPattern("O"); + f = (locale != null) ? builder.toFormatter(locale) : builder.toFormatter(); assertEquals(f.format(odt), expected); assertEquals(f.format(zdt), expected); assertEquals(f.parse(expected, ZoneOffset::from), offset); } + } //----------------------------------------------------------------------- --- /dev/null 2019-07-02 19:28:39.000000000 +0530 +++ new/test/jdk/java/time/test/java/time/format/TestOffsetPrinterParser.java 2019-07-02 19:28:36.621538100 +0530 @@ -0,0 +1,140 @@ +/* + * Copyright (c) 2012, 2019, 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 + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 8154520 + * @summary Tests for DateTimeFormatterBuilder.appendLocalizedOffset + * @modules jdk.localedata + */ + +package test.java.time.format; + +import static org.testng.Assert.assertEquals; + +import java.time.LocalDateTime; +import java.time.OffsetDateTime; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeFormatterBuilder; +import java.time.format.TextStyle; +import java.util.Locale; + +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +/** + * Test DateTimeFormatterBuilder.appendOffset(). + */ +@Test +public class TestOffsetPrinterParser { + + private static final LocalDateTime DT_2012_06_30_12_30_40 = LocalDateTime.of(2012, 6, 30, 12, 30, 40); + + private static final ZoneOffset OFFSET_UTC = ZoneOffset.UTC; + private static final ZoneOffset OFFSET_P012345 = ZoneOffset.ofHoursMinutesSeconds(1, 23, 45); + private static final ZoneOffset OFFSET_P000045 = ZoneOffset.ofHoursMinutesSeconds(0, 0, 45); + private static final ZoneOffset OFFSET_M0100 = ZoneOffset.ofHours(-1); + private static final ZoneOffset OFFSET_M0123 = ZoneOffset.ofHoursMinutes(-1, -23); + private static final ZoneOffset OFFSET_M012345 = ZoneOffset.ofHoursMinutesSeconds(-1, -23, -45); + private static final ZoneOffset OFFSET_M000045 = ZoneOffset.ofHoursMinutesSeconds(0, 0, -45); + + private static final Locale LOCALE_AM = new Locale("am"); + private static final Locale LOCALE_AR = new Locale("ar"); + private static final Locale LOCALE_BG = new Locale("bg"); + private static final Locale LOCALE_CN = Locale.CHINESE; + private static final Locale LOCALE_EN = Locale.ENGLISH; + private static final Locale LOCALE_EO = new Locale("eo"); + private static final Locale LOCALE_FR = Locale.FRANCE; + private static final Locale LOCALE_GA = new Locale("ga"); + private static final Locale LOCALE_KM = new Locale("km"); + private static final Locale LOCALE_ML = new Locale("ml"); + private static final Locale LOCALE_MR = new Locale("mr"); + private static final Locale LOCALE_SE = new Locale("se"); + private static final Locale LOCALE_YO_BJ = new Locale("yo", "BJ"); + + private DateTimeFormatterBuilder builder; + + @BeforeMethod + public void setUp() { + builder = new DateTimeFormatterBuilder(); + } + + @DataProvider(name="print_localized_custom_locale") + Object[][] data_print_localized_custom_locale() { + return new Object[][] { + {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M0123, LOCALE_AM, "\u1302 \u12a4\u121d \u1272-1:23"}, + {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M0100, LOCALE_AR, "\u063a\u0631\u064a\u0646\u062a\u0634-01:00"}, + {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P012345, LOCALE_BG, "\u0413\u0440\u0438\u043d\u0443\u0438\u0447+01:23:45"}, + {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M000045, LOCALE_CN, "GMT-00:00:45"}, + {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P012345, LOCALE_EN, "GMT+01:23:45"}, + {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M012345, LOCALE_EO, "GMT-1:23:45"}, + {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P000045, LOCALE_FR, "UTC+00:00:45"}, + {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M0100, LOCALE_GA, "MAG-1"}, + {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_UTC, LOCALE_KM, "\u1798\u17c9\u17c4\u1784\u200b\u179f\u1780\u179b"}, + {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_UTC, LOCALE_ML, "\u0d1c\u0d3f\u0d0e\u0d02\u0d1f\u0d3f"}, + {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P012345, LOCALE_MR, "[GMT]+01:23:45"}, + {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_P012345, LOCALE_SE, "UTC+1:23:45"}, + {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M0100, LOCALE_YO_BJ, "WAT-01:00"} + }; + } + + @Test(dataProvider="print_localized_custom_locale") + public void test_print_localized_custom_locale(TextStyle style, LocalDateTime ldt, ZoneOffset offset, Locale locale, String expected) { + + OffsetDateTime odt = OffsetDateTime.of(ldt, offset); + ZonedDateTime zdt = ldt.atZone(offset); + + builder = new DateTimeFormatterBuilder().appendLocalizedOffset(style); + DateTimeFormatter f = (locale != null) ? builder.toFormatter(locale) : builder.toFormatter(); + assertEquals(f.format(odt), expected); + assertEquals(f.format(zdt), expected); + assertEquals(f.parse(expected, ZoneOffset::from), offset); + + if (style == TextStyle.FULL) { + builder = new DateTimeFormatterBuilder().appendPattern("ZZZZ"); + f = (locale != null) ? builder.toFormatter(locale) : builder.toFormatter(); + assertEquals(f.format(odt), expected); + assertEquals(f.format(zdt), expected); + assertEquals(f.parse(expected, ZoneOffset::from), offset); + + builder = new DateTimeFormatterBuilder().appendPattern("OOOO"); + f = (locale != null) ? builder.toFormatter(locale) : builder.toFormatter(); + assertEquals(f.format(odt), expected); + assertEquals(f.format(zdt), expected); + assertEquals(f.parse(expected, ZoneOffset::from), offset); + } + + if (style == TextStyle.SHORT) { + builder = new DateTimeFormatterBuilder().appendPattern("O"); + f = (locale != null) ? builder.toFormatter(locale) : builder.toFormatter(); + assertEquals(f.format(odt), expected); + assertEquals(f.format(zdt), expected); + assertEquals(f.parse(expected, ZoneOffset::from), offset); + } + + } + +}