/* * Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/** * @test * @bug 4823811 8008577 * @summary Confirm that text which includes numbers with a trailing minus sign is parsed correctly. * @modules jdk.localedata * @run main/othervm -Duser.timezone=GMT+09:00 -Djava.locale.providers=JRE,SPI Bug4823811
*/
/* * I don't use static import here intentionally so that this test program * can be run on JDK 1.4.2.
*/ privatestaticint ERA = Calendar.ERA; privatestaticint BC = GregorianCalendar.BC; // private static int JAN = Calendar.JANUARY; // private static int FEB = Calendar.FEBRUARY; // private static int MAR = Calendar.MARCH; privatestaticint APR = Calendar.APRIL; privatestaticint MAY = Calendar.MAY; privatestaticint JUN = Calendar.JUNE; privatestaticint JUL = Calendar.JULY; // private static int AUG = Calendar.AUGUST; // private static int SEP = Calendar.SEPTEMBER; // private static int OCT = Calendar.OCTOBER; // private static int NOV = Calendar.NOVEMBER; // private static int DEC = Calendar.DECEMBER;
privatestatic String[] patterns = { "yyyy MMMM d H m s", "yyyy MM dd hh mm ss",
/* * Because 1-based HOUR_OF_DAY, 1-based HOUR, MONTH, and YEAR fields * are parsed using different code from the code for other numeric * fields, I prepared YEAR-preceding patterns and SECOND-preceding * patterns.
*/ "yyyy M d h m s", " yyyy M d h m s", "yyyy M d h m s ",
"s m h d M yyyy", " s m h d M yyyy", "s m h d M yyyy ",
};
privatestaticchar originalMinusSign1 = ':'; privatestaticchar originalMinusSign2 = '\uff0d'; // fullwidth minus privatestatic String[] delimiters = {"-", "/", ":", "/", "\uff0d", "/"}; privatestatic String[][] specialDelimiters = { // for Arabic formatter and modified English formatter
{"--", "-/", "::", ":/", "\uff0d\uff0d", "\uff0d/"},
// for English formatter and modified Arabic formatter
{"--", "/-", "::", "/:", "\uff0d\uff0d", "/\uff0d"},
};
try { /* * Test SimpleDateFormat.parse() and format() for original * SimpleDateFormat instances
*/
testDateFormat1();
/* * Test SimpleDateFormat.parse() and format() for modified * SimpleDateFormat instances using an original minus sign, * pattern, and diffenrent month names in DecimalFormat
*/
testDateFormat2();
/* * Test SimpleDateFormat.parse() and format() for modified * SimpleDateFormat instances using a fullwidth minus sign
*/
testDateFormat3();
/* * Just to confirm that regressions aren't introduced in * DecimalFormat. This cannot happen, though. Because I didn't * change DecimalFormat at all.
*/
testNumberFormat();
} catch (Exception e) {
err = true;
System.err.println("Unexpected exception: " + e);
} finally {
Locale.setDefault(defaultLocale);
TimeZone.setDefault(defaultTimeZone);
if (err) {
System.err.println(BORDER + " Test failed."); thrownew RuntimeException("Date/Number formatting/parsing error.");
} else {
System.out.println(BORDER + " Test passed.");
}
}
}
// // DateFormat test // privatestaticvoid testDateFormat1() { for (int i = 0; i < patterns.length; i++) {
System.out.println(BORDER); for (int j = 0; j <= 1; j++) { // Generate a pattern
String pattern = patterns[i].replaceAll(" ", delimiters[j]);
System.out.println("Pattern=\"" + pattern + "\"");
System.out.println("*** DateFormat.format test in ar_EG");
testDateFormatFormattingInRTL(pattern, i, j, null, localeEG, false);
System.out.println("*** DateFormat.parse test in ar_EG");
testDateFormatParsingInRTL(pattern, i, j, null, localeEG, false);
System.out.println("*** DateFormat.format test in en_US");
testDateFormatFormattingInLTR(pattern, i, j, null, localeUS, true);
System.out.println("*** DateFormat.parse test in en_US");
testDateFormatParsingInLTR(pattern, i, j, null, localeUS, true);
}
}
}
privatestaticvoid testDateFormat2() { /* * modified ar_EG Date&Time formatter : * minus sign: ':' * pattern: "#,##0.###" * month names: In Arabic * * modified en_US Date&Time formatter : * minus sign: ':' * pattern: "#,##0.###;#,##0.###-" * month names: In English
*/
DecimalFormat dfEG = (DecimalFormat)NumberFormat.getInstance(localeEG);
DecimalFormat dfUS = (DecimalFormat)NumberFormat.getInstance(localeUS);
for (int i = 0; i < patterns.length; i++) {
System.out.println(BORDER); for (int j = 4; j <= 5; j++) { // Generate a pattern
String pattern = patterns[i].replaceAll(" ", delimiters[j]);
System.out.println("Pattern=\"" + pattern + "\"");
System.out.println("*** DateFormat.format test in modified ar_EG");
testDateFormatFormattingInRTL(pattern, i, j, dfEG, localeEG, false);
System.out.println("*** DateFormat.parse test in modified ar_EG");
testDateFormatParsingInRTL(pattern, i, j, dfEG, localeEG, false);
System.out.println("*** DateFormat.format test in modified en_US");
testDateFormatFormattingInLTR(pattern, i, j, dfUS, localeUS, true);
System.out.println("*** DateFormat.parse test in modified en_US");
testDateFormatParsingInLTR(pattern, i, j, dfUS, localeUS, true);
}
}
}
privatestaticvoid testDateFormatFormattingInRTL(String pattern, int basePattern, int delimiter,
NumberFormat nf,
Locale locale, boolean useEnglishMonthName) {
Locale.setDefault(locale);
SimpleDateFormat sdf = new SimpleDateFormat(pattern); if (nf != null) {
sdf.setNumberFormat(nf);
} for (int i = 0; i < datesToParse[basePattern].length; i++) { if (datesEG[basePattern][i] == null) { continue;
}
privatestaticvoid testDateFormatFormattingInLTR(String pattern, int basePattern, int delimiter,
NumberFormat nf,
Locale locale, boolean useEnglishMonthName) {
Locale.setDefault(locale);
SimpleDateFormat sdf = new SimpleDateFormat(pattern); if (nf != null) {
sdf.setNumberFormat(nf);
} for (int i = 0; i < datesToParse[basePattern].length; i++) { if (datesUS[basePattern][i] == null) { continue;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.