Path: blob/master/test/jdk/java/text/Format/DateFormat/Bug4396385.java
41152 views
/*1* Copyright (c) 2010, 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 439638526* @summary Make sure to detect invalid values for 1-based hour formats.27*/2829import java.text.*;30import java.util.*;3132public class Bug4396385 {33private static int errorCount = 0;34private static String[][] data = {35{ "hh:mma", "-1:30AM" },36{ "hh:mma", "00:30AM" },37{ "hh:mma", "13:30AM" },38{ "kk:mm", "-1:12" },39{ "kk:mm", "00:12" },40{ "kk:mm", "25:12" },41};4243public static void main(String[] args) {44TimeZone tz = TimeZone.getDefault();45// Use "GMT" to avoid any surprises related to offset46// transitions.47TimeZone.setDefault(TimeZone.getTimeZone("GMT"));4849try {50for (String[] item : data) {51test(item[0], item[1]);52}53} finally {54// Restore the default time zone55TimeZone.setDefault(tz);56}5758if (errorCount > 0) {59throw new RuntimeException("Failed with " + errorCount + " error(s).");60}61}6263private static void test(String pattern, String src) {64SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.US);65sdf.setLenient(false);66ParsePosition pos = new ParsePosition(0);67System.out.printf("parse: \"%s\" with \"%s\"", src, pattern);68Date date = sdf.parse(src, pos);69System.out.printf(": date = %s, errorIndex = %d", date, pos.getErrorIndex());70if (date != null || pos.getErrorIndex() == -1) {71System.out.println(": failed");72errorCount++;73} else {74System.out.println(": passed");75}76}77}787980