1 /*
   2  * Copyright (c) 2007, 2016, 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 6530336 6537997 8008577
  27  * @run main/othervm -Djava.locale.providers=COMPAT,SPI Bug6530336
  28  */
  29 
  30 import java.text.SimpleDateFormat;
  31 import java.util.Calendar;
  32 import java.util.Date;
  33 import java.util.Locale;
  34 import java.util.TimeZone;
  35 
  36 public class Bug6530336 {
  37 
  38     public static void main(String[] args) throws Exception {
  39         Locale defaultLocale = Locale.getDefault();
  40         TimeZone defaultTimeZone = TimeZone.getDefault();
  41 
  42         boolean err = false;
  43 
  44         try {
  45             Locale locales[] = Locale.getAvailableLocales();
  46             Locale locale_Japan = new Locale("ja", "JP", "JP");
  47             TimeZone timezone_LA = TimeZone.getTimeZone("America/Los_Angeles");
  48             TimeZone.setDefault(timezone_LA);
  49 
  50             TimeZone timezones[] = {
  51                 TimeZone.getTimeZone("America/New_York"),
  52                 TimeZone.getTimeZone("America/Denver"),
  53             };
  54 
  55             String[] expected = {
  56                 "Sun Jul 15 12:00:00 PDT 2007",
  57                 "Sun Jul 15 14:00:00 PDT 2007",
  58             };
  59 
  60             Date[] dates = new Date[2];
  61 
  62             for (int i = 0; i < locales.length; i++) {
  63                 if (locales[i].getLanguage().equals("th") ||
  64                     locales[i].equals(locale_Japan)) {
  65                     continue;
  66                 }
  67 
  68                 Locale.setDefault(locales[i]);
  69 
  70                 for (int j = 0; j < timezones.length; j++) {
  71                     Calendar cal = Calendar.getInstance(timezones[j]);
  72                     cal.set(2007, 6, 15, 15, 0, 0);
  73                     dates[j] = cal.getTime();
  74                 }
  75 
  76                 SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
  77 
  78                 for (int j = 0; j < timezones.length; j++) {
  79                     sdf.setTimeZone(timezones[j]);
  80                     String date = sdf.format(dates[j]);
  81                     sdf.setTimeZone(timezone_LA);
  82                     String date_LA = sdf.parse(date).toString();
  83 
  84                     if (!expected[j].equals(date_LA)) {
  85                         System.err.println("Got wrong Pacific time (" +
  86                             date_LA + ") for (" + date + ") in " + locales[i] +
  87                             " in " + timezones[j] +
  88                             ".\nExpected=" + expected[j]);
  89                         err = true;
  90                     }
  91                 }
  92             }
  93         }
  94         catch (Exception e) {
  95             e.printStackTrace();
  96             err = true;
  97         }
  98         finally {
  99             Locale.setDefault(defaultLocale);
 100             TimeZone.setDefault(defaultTimeZone);
 101 
 102             if (err) {
 103                 throw new RuntimeException("Failed.");
 104             }
 105         }
 106     }
 107 
 108 }