Path: blob/master/test/jdk/java/text/Format/DateFormat/Bug8141243.java
41152 views
/*1* Copyright (c) 2015, 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* @run main/othervm -Djava.locale.providers=COMPAT Bug814124329*/3031import java.text.DateFormat;32import java.text.ParseException;33import java.text.SimpleDateFormat;34import java.util.ArrayList;35import java.util.Date;36import java.util.List;37import java.util.Locale;38import java.util.TimeZone;39import static java.util.TimeZone.*;4041public class Bug8141243 {42public static void main(String[] args) {43TimeZone UTC = TimeZone.getTimeZone("UTC");44TimeZone initTz = TimeZone.getDefault();4546List<String> errors = new ArrayList<>();47try {48TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));49for (Locale locale : DateFormat.getAvailableLocales()) {50// exclude any locales which localize "UTC".51String utc = UTC.getDisplayName(false, SHORT, locale);52if (!"UTC".equals(utc)) {53System.out.println("Skipping " + locale + " due to localized UTC name: " + utc);54continue;55}56SimpleDateFormat fmt = new SimpleDateFormat("z", locale);57try {58Date date = fmt.parse("UTC");59// Parsed one may not exactly be UTC. Universal, UCT, etc. are equivalents.60if (!fmt.getTimeZone().getID().matches("(Etc/)?(UTC|Universal|UCT|Zulu)")) {61errors.add("timezone: " + fmt.getTimeZone().getID()62+ ", locale: " + locale);63}64} catch (ParseException e) {65errors.add("parse exception: " + e + ", locale: " + locale);66}67}68} finally {69// Restore the default time zone70TimeZone.setDefault(initTz);71}7273if (!errors.isEmpty()) {74System.out.println("Got unexpected results:");75for (String s : errors) {76System.out.println(" " + s);77}78throw new RuntimeException("Test failed.");79} else {80System.out.println("Test passed.");81}82}83}848586