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