< prev index next >

test/jdk/java/text/Format/DateFormat/DateFormatRegression.java

Print this page
8247706: Unintentional use of new Date(year...) with absolute year
Reviewed-by: naoto, rriggs, scolebourne
   1 /*
   2  * Copyright (c) 1998, 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  */


1050         if (i < 0 || j >= 0) {
1051             errln("FAIL: getTimeInstance().format(d) => \"" +
1052                   s + "\"");
1053         }
1054     }
1055 
1056     /**
1057      * Test whether SimpleDataFormat (DateFormatSymbols) can format/parse
1058      * non-localized time zones.
1059      */
1060     public void Test4261506() {
1061         Locale savedLocale = Locale.getDefault();
1062         TimeZone savedTimeZone = TimeZone.getDefault();
1063         Locale.setDefault(Locale.JAPAN);
1064 
1065         // XXX: Test assumes "PST" is not TimeZoneNames_ja. Need to
1066         // pick up another time zone when L10N is done to that file.
1067         TimeZone.setDefault(TimeZone.getTimeZone("PST"));
1068         SimpleDateFormat fmt = new SimpleDateFormat("yy/MM/dd hh:ss zzz", Locale.JAPAN);
1069         @SuppressWarnings("deprecation")
1070         String result = fmt.format(new Date(1999, 0, 1));
1071         logln("format()=>" + result);
1072         if (!result.endsWith("PST")) {
1073             errln("FAIL: SimpleDataFormat.format() did not retrun PST");
1074         }
1075 
1076         Date d = null;
1077         try {
1078             d = fmt.parse("99/1/1 10:10 PST");
1079         } catch (ParseException e) {
1080             errln("FAIL: SimpleDataFormat.parse() could not parse PST");
1081         }
1082 
1083         result = fmt.format(d);
1084         logln("roundtrip:" + result);
1085         if (!result.equals("99/01/01 10:10 PST")) {
1086             errln("FAIL: SimpleDataFomat timezone roundtrip failed");
1087         }
1088 
1089         Locale.setDefault(savedLocale);
1090         TimeZone.setDefault(savedTimeZone);
   1 /*
   2  * Copyright (c) 1998, 2020, 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  */


1050         if (i < 0 || j >= 0) {
1051             errln("FAIL: getTimeInstance().format(d) => \"" +
1052                   s + "\"");
1053         }
1054     }
1055 
1056     /**
1057      * Test whether SimpleDataFormat (DateFormatSymbols) can format/parse
1058      * non-localized time zones.
1059      */
1060     public void Test4261506() {
1061         Locale savedLocale = Locale.getDefault();
1062         TimeZone savedTimeZone = TimeZone.getDefault();
1063         Locale.setDefault(Locale.JAPAN);
1064 
1065         // XXX: Test assumes "PST" is not TimeZoneNames_ja. Need to
1066         // pick up another time zone when L10N is done to that file.
1067         TimeZone.setDefault(TimeZone.getTimeZone("PST"));
1068         SimpleDateFormat fmt = new SimpleDateFormat("yy/MM/dd hh:ss zzz", Locale.JAPAN);
1069         @SuppressWarnings("deprecation")
1070         String result = fmt.format(new Date(1999 - 1900, 0, 1));
1071         logln("format()=>" + result);
1072         if (!result.endsWith("PST")) {
1073             errln("FAIL: SimpleDataFormat.format() did not retrun PST");
1074         }
1075 
1076         Date d = null;
1077         try {
1078             d = fmt.parse("99/1/1 10:10 PST");
1079         } catch (ParseException e) {
1080             errln("FAIL: SimpleDataFormat.parse() could not parse PST");
1081         }
1082 
1083         result = fmt.format(d);
1084         logln("roundtrip:" + result);
1085         if (!result.equals("99/01/01 10:10 PST")) {
1086             errln("FAIL: SimpleDataFomat timezone roundtrip failed");
1087         }
1088 
1089         Locale.setDefault(savedLocale);
1090         TimeZone.setDefault(savedTimeZone);
< prev index next >