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 Timezone returned after parsing a date with SimpleDateFormat is correct. 28 * Description: When parsing the virtual timezone "UTC" with java.text.SimpleDateFormat, 29 * the timezone is set to the first timezone that matches an actual timezone in the UTC group, which is Antarctica/Troll. 30 * When comparing this timezone with the result of TimeZone.getTimeZone("UTC"), we fail. 31 * 32 */ 33 34 import java.text.ParseException; 35 import java.text.SimpleDateFormat; 36 import java.util.Calendar; 37 import java.util.Date; 38 import java.util.GregorianCalendar; 39 import java.util.TimeZone; 40 41 public class Bug8141243 { 42 private static int errorCount = 0; 43 private static final String[] TIMEZONES = { 44 45 "UTC", // Coordinated Universal Time 46 "IST", // Indian Standard Time 47 "PST", // Pacific Standard Time 48 "MST", // Mountain Standard Time 49 "CST", // Central Standard Time 50 "EST", // Eastern Standard Time 51 "HST", // Hawaii Standard Time 52 "AST", // Atlantic Standard Time 53 "NST", // Newfoundland Standard Time 54 "CET", // Central European Time 55 "GMT", // Greenwich Mean Time 56 "WET", // Western European Time 57 "JST", // Japan Standard Time 58 "EET", // Eastern European Time 59 "CST", // China Standard Time 60 "ART", // Argentine Time 61 "EAT", // Eastern African Time 62 "CAT", // Central African Time 63 "ACT", // Acre Time 30 64 "ECT", // Ecuador Time 65 "Greenwich Mean Time" 66 67 }; 68 69 public static void main(String[] args) { 70 String datePattern = "z"; 71 SimpleDateFormat simpleDateFormat = new SimpleDateFormat(datePattern); 72 long now = System.currentTimeMillis(); 73 Date date; 74 Calendar calendar; 75 TimeZone timeZone; 76 77 System.out.println("Running Total " + TIMEZONES.length + " tests..."); 78 for (int i = 0; i < TIMEZONES.length; i++) { 79 try { 80 date = simpleDateFormat.parse(TIMEZONES[i]); 81 calendar = new GregorianCalendar(simpleDateFormat.getTimeZone()); 82 calendar.setTime(date); 83 timeZone = TimeZone.getTimeZone(TIMEZONES[i]); 84 85 if (!timeZone.getID().equalsIgnoreCase(calendar.getTimeZone().getID())) { 86 throw new Exception(" *** TimeZone ID mismatch for test " + (i + 1) + " *** " + timeZone.getID() 87 + " != " + calendar.getTimeZone().getID()); 88 } 89 if (timeZone.getOffset(now) != calendar.getTimeZone().getOffset(now)) { 90 throw new Exception(" *** TimeZone Offset mismatch for test " + (i + 1) + " *** " + timeZone.getID() 91 + " != " + calendar.getTimeZone().getID()); 92 } 93 System.out.println("Test-" + (i + 1) + " : Works As Expected"); 94 95 } catch (Exception e) { 96 if (e instanceof ParseException) { 97 System.err.println("Exception occured while Parsing timezone : " + TIMEZONES[i]); 98 } else { 99 System.err.println(e.getMessage()); 100 } 101 errorCount++; 102 } 103 } 104 if (errorCount > 0) { 105 throw new RuntimeException("Test failed with " + errorCount + " error(s)..."); 106 } 107 } 108 }