Path: blob/jdk8u272-b10-aarch32-20201026/jdk/test/java/text/Format/DateFormat/Bug8141243.java
48795 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 814124326* @summary Make sure that SimpleDateFormat parses "UTC" as the UTC time zone.27* @run main Bug814124328*/2930import java.text.DateFormat;31import java.text.ParseException;32import java.text.SimpleDateFormat;33import java.util.ArrayList;34import java.util.Date;35import java.util.List;36import java.util.Locale;37import java.util.TimeZone;38import static java.util.TimeZone.*;3940public class Bug8141243 {41public static void main(String[] args) {42TimeZone UTC = TimeZone.getTimeZone("UTC");43TimeZone initTz = TimeZone.getDefault();4445List<String> errors = new ArrayList<>();46try {47TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));48for (Locale locale : DateFormat.getAvailableLocales()) {49// exclude any locales which localize "UTC".50String utc = UTC.getDisplayName(false, SHORT, locale);51if (!"UTC".equals(utc)) {52System.out.println("Skipping " + locale + " due to localized UTC name: " + utc);53continue;54}55SimpleDateFormat fmt = new SimpleDateFormat("z", locale);56try {57Date date = fmt.parse("UTC");58// Parsed one may not exactly be UTC. Universal, UCT, etc. are equivalents.59if (!fmt.getTimeZone().getID().matches("(Etc/)?(UTC|Universal|UCT|Zulu)")) {60errors.add("timezone: " + fmt.getTimeZone().getID()61+ ", locale: " + locale);62}63} catch (ParseException e) {64errors.add("parse exception: " + e + ", locale: " + locale);65}66}67} finally {68// Restore the default time zone69TimeZone.setDefault(initTz);70}7172if (!errors.isEmpty()) {73System.out.println("Got unexpected results:");74for (String s : errors) {75System.out.println(" " + s);76}77throw new RuntimeException("Test failed.");78} else {79System.out.println("Test passed.");80}81}82}838485