undefect. CWE-407 — 63 sites patched across 27 ecosystems
Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com Patches, unit tests, benchmarks, whitepaper, and outreach briefs. Public domain — no copyright claimed. Use freely.
This commit is contained in:
commit
0a580b313d
70422 changed files with 17213626 additions and 0 deletions
367
test/jdk/java/text/Format/DateFormat/Bug4322313.java
Normal file
367
test/jdk/java/text/Format/DateFormat/Bug4322313.java
Normal file
|
|
@ -0,0 +1,367 @@
|
|||
/*
|
||||
* Copyright (c) 2001, 2023, 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 4322313 4833268 6302990 6304305
|
||||
* @summary Make sure that new implementation for
|
||||
* SimpleDateFormat.parse('z' or 'Z') and format('z' or 'Z') work correctly.
|
||||
* @run junit Bug4322313
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
public class Bug4322313 {
|
||||
|
||||
@Test
|
||||
public void Test4322313() {
|
||||
Locale savedLocale = Locale.getDefault();
|
||||
TimeZone savedTimeZone = TimeZone.getDefault();
|
||||
boolean err = false;
|
||||
long mpm = 60 * 1000; /* Milliseconds per a minute */
|
||||
|
||||
Locale[] locs = {Locale.US, Locale.JAPAN, Locale.UK, Locale.of("ar")};
|
||||
|
||||
String[] formats = {
|
||||
"z",
|
||||
"Z",
|
||||
};
|
||||
|
||||
Object[][] valids = {
|
||||
/* given ID offset format('z'), ('Z') index */
|
||||
{"GMT+03:04", -184L * mpm, "GMT+03:04", "+0304", 9},
|
||||
{"GMT+13:42", -822L * mpm, "GMT+13:42", "+1342", 9},
|
||||
{"GMT+00:00", 0L, "GMT+00:00", "+0000", 9},
|
||||
{"GMT+1:11", -71L * mpm, "GMT+01:11", "+0111", 8},
|
||||
{"GMT +13:42", 0L, "GMT", "+0000", 3},
|
||||
{" GMT", 0L, "GMT", "+0000", 4},
|
||||
{"+0304", -184L * mpm, "GMT+03:04", "+0304", 5},
|
||||
{"+1342", -822L * mpm, "GMT+13:42", "+1342", 5},
|
||||
{"+0000", 0L, "GMT+00:00", "+0000", 5},
|
||||
{" +1342", -822L * mpm, "GMT+13:42", "+1342", 6},
|
||||
/* ISO-LATIN-1 digits */
|
||||
{"GMT+\u0030\u0031:\u0032\u0033", -83L * mpm, "GMT+01:23", "+0123", 9},
|
||||
|
||||
/* In fact, this test case is skipped because TimeZone class can't
|
||||
* recognize TimeZone IDs like "+00234" or "-00234".
|
||||
*/
|
||||
{"+00234", -23L * mpm, "GMT+00:23", "+0023", 5},
|
||||
|
||||
{"GMT-03:04", 184L * mpm, "GMT-03:04", "-0304", 9},
|
||||
{"GMT-13:42", 822L * mpm, "GMT-13:42", "-1342", 9},
|
||||
{"GMT-00:00", 0L, "GMT+00:00", "+0000", 9},
|
||||
{"GMT-1:11", 71L * mpm, "GMT-01:11", "-0111", 8},
|
||||
{"GMT -13:42", 0L, "GMT", "+0000", 3},
|
||||
{"-0304", 184L * mpm, "GMT-03:04", "-0304", 5},
|
||||
{"-1342", 822L * mpm, "GMT-13:42", "-1342", 5},
|
||||
{" -1342", 822L * mpm, "GMT-13:42", "-1342", 6},
|
||||
/* ISO-LATIN-1 digits */
|
||||
{"GMT-\u0030\u0031:\u0032\u0033", 83L * mpm, "GMT-01:23", "-0123", 9},
|
||||
/* In fact, this test case is skipped because TimeZone class can't
|
||||
* recognize TimeZone IDs like "+00234" or "-00234".
|
||||
*/
|
||||
{"-00234", 23L * mpm, "GMT+00:23", "-0023", 5},
|
||||
};
|
||||
|
||||
Object[][] invalids = {
|
||||
/* given ID error index */
|
||||
{"GMT+8", 5},
|
||||
{"GMT+18", 6},
|
||||
{"GMT+208", 6},
|
||||
{"GMT+0304", 6},
|
||||
{"GMT+42195", 5},
|
||||
{"GMT+5:8", 7},
|
||||
{"GMT+23:60", 8},
|
||||
{"GMT+11:1", 8},
|
||||
{"GMT+24:13", 5},
|
||||
{"GMT+421:950", 5},
|
||||
{"GMT+0a:0A", 5},
|
||||
{"GMT+ 13:42", 4},
|
||||
{"GMT+13 :42", 6},
|
||||
{"GMT+13: 42", 7},
|
||||
{"GMT+-13:42", 4},
|
||||
{"G M T", 0},
|
||||
{"+8", 2},
|
||||
{"+18", 3},
|
||||
{"+208", 4},
|
||||
{"+2360", 4},
|
||||
{"+2413", 2},
|
||||
{"+42195", 2},
|
||||
{"+0AbC", 2},
|
||||
{"+ 1342", 1},
|
||||
{"+-1342", 1},
|
||||
{"1342", 0},
|
||||
/* Arabic-Indic digits */
|
||||
{"GMT+\u0660\u0661:\u0662\u0663", 4},
|
||||
/* Extended Arabic-Indic digits */
|
||||
{"GMT+\u06f0\u06f1:\u06f2\u06f3", 4},
|
||||
/* Devanagari digits */
|
||||
{"GMT+\u0966\u0967:\u0968\u0969", 4},
|
||||
/* Fullwidth digits */
|
||||
{"GMT+\uFF10\uFF11:\uFF12\uFF13", 4},
|
||||
|
||||
{"GMT-8", 5},
|
||||
{"GMT-18", 6},
|
||||
{"GMT-208", 6},
|
||||
{"GMT-0304", 6},
|
||||
{"GMT-42195", 5},
|
||||
{"GMT-5:8", 7},
|
||||
{"GMT-23:60", 8},
|
||||
{"GMT-11:1", 8},
|
||||
{"GMT-24:13", 5},
|
||||
{"GMT-421:950", 5},
|
||||
{"GMT-0a:0A", 5},
|
||||
{"GMT- 13:42", 4},
|
||||
{"GMT-13 :42", 6},
|
||||
{"GMT-13: 42", 7},
|
||||
{"GMT-+13:42", 4},
|
||||
{"-8", 2},
|
||||
{"-18", 3},
|
||||
{"-208", 4},
|
||||
{"-2360", 4},
|
||||
{"-2413", 2},
|
||||
{"-42195", 2},
|
||||
{"-0AbC", 2},
|
||||
{"- 1342", 1},
|
||||
{"--1342", 1},
|
||||
{"-802", 2},
|
||||
/* Arabic-Indic digits */
|
||||
{"GMT-\u0660\u0661:\u0662\u0663", 4},
|
||||
/* Extended Arabic-Indic digits */
|
||||
{"GMT-\u06f0\u06f1:\u06f2\u06f3", 4},
|
||||
/* Devanagari digits */
|
||||
{"GMT-\u0966\u0967:\u0968\u0969", 4},
|
||||
/* Fullwidth digits */
|
||||
{"GMT-\uFF10\uFF11:\uFF12\uFF13", 4},
|
||||
};
|
||||
|
||||
try {
|
||||
for (int i=0; i < locs.length; i++) {
|
||||
Locale locale = locs[i];
|
||||
Locale.setDefault(locale);
|
||||
|
||||
for (int j=0; j < formats.length; j++) {
|
||||
TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(formats[j]);
|
||||
Date date;
|
||||
|
||||
/* Okay case */
|
||||
for (int k=0; k < valids.length; k++) {
|
||||
ParsePosition pos = new ParsePosition(0);
|
||||
try {
|
||||
date = sdf.parse((String)valids[k][0], pos);
|
||||
}
|
||||
catch (Exception e) {
|
||||
err = true;
|
||||
System.err.println("\tParse Error [Locale=" +
|
||||
locale + ", " + formats[j] +
|
||||
"/\"" + valids[k][0] +
|
||||
"\"] Unexpected Exception occurred: " + e);
|
||||
continue;
|
||||
}
|
||||
|
||||
int offset = pos.getIndex();
|
||||
if (offset != ((Integer)valids[k][4]).intValue()) {
|
||||
err = true;
|
||||
System.err.println("\tParse Error [Locale=" +
|
||||
locale + ", " + formats[j] +
|
||||
"/\"" + valids[k][0] +
|
||||
"\"] invalid index: expected:" + valids[k][4] +
|
||||
", got:" + offset);
|
||||
}
|
||||
|
||||
if (date.getTime() != ((Long)valids[k][1]).longValue()) {
|
||||
err = true;
|
||||
System.err.println("\tParse Error [Locale=" +
|
||||
locale + ", " + formats[j] +
|
||||
"/\"" + valids[k][0] +
|
||||
"\"] expected:" + valids[k][1] +
|
||||
", got:" + date.getTime() + ", " + date);
|
||||
} else {
|
||||
/*
|
||||
System.out.println("\tParse Okay [Locale=" +
|
||||
locale) + ", " + formats[j] +
|
||||
"/\"" + valids[k][0] +
|
||||
"\"] expected:" + valids[k][1] +
|
||||
", got:" + date.getTime() + ", " + date);
|
||||
*/
|
||||
|
||||
try {
|
||||
date = sdf.parse((String)valids[k][0]);
|
||||
}
|
||||
catch (Exception e) {
|
||||
err = true;
|
||||
System.err.println("\tParse Error [Locale=" +
|
||||
locale + ", " + formats[j] +
|
||||
"/\"" + valids[k][0] +
|
||||
"\"] Unexpected Exception occurred: " + e);
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Since TimeZone.getTimeZone() don't treat
|
||||
* "+00234" or "-00234" as a valid ID, skips.
|
||||
*/
|
||||
if (((String)valids[k][0]).length() == 6) {
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Since TimeZone.getTimeZone() don't recognize
|
||||
* +hhmm/-hhmm format, add "GMT" as prefix.
|
||||
*/
|
||||
sdf.setTimeZone(TimeZone.getTimeZone(
|
||||
(((((String)valids[k][0]).charAt(0) != 'G') ?
|
||||
"GMT" : "") + valids[k][0])));
|
||||
StringBuffer s = new StringBuffer();
|
||||
sdf.format(date, s, new FieldPosition(0));
|
||||
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||
|
||||
String got = s.toString();
|
||||
String expected = (String)valids[k][2+j];
|
||||
if (!got.equals(expected) &&
|
||||
// special case to allow the difference between
|
||||
// DateFormatSymbols.getZoneStrings() and
|
||||
// TimeZone.getDisplayName() for "GMT+-00:00"
|
||||
!(got.equals("GMT-00:00") &&
|
||||
expected.equals("GMT+00:00"))) {
|
||||
err = true;
|
||||
System.err.println("\tFormat Error [Locale=" +
|
||||
locale + ", " +
|
||||
formats[j] + "/\"" + valids[k][0] +
|
||||
"\"] expected:" + valids[k][2+j] +
|
||||
", got:" + s + ", " + date);
|
||||
} else {
|
||||
/*
|
||||
System.out.println("\tFormat Okay [Locale=" +
|
||||
locale + ", " +
|
||||
formats[j] + "/\"" + valids[k][0] +
|
||||
"\"] expected:" + valids[k][2+j] +
|
||||
", got:" + s + ", " + date);
|
||||
*/
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Error case 1
|
||||
* using SimpleDateFormat.parse(String, ParsePosition)
|
||||
*/
|
||||
for (int k=0; k < invalids.length; k++) {
|
||||
ParsePosition pos = new ParsePosition(0);
|
||||
try {
|
||||
date = sdf.parse((String)invalids[k][0], pos);
|
||||
if (date != null) {
|
||||
err = true;
|
||||
System.err.println("\tParse Error [Locale=" +
|
||||
locale + ", " + formats[j] +
|
||||
"/\"" + invalids[k][0] +
|
||||
"\"] expected:null , got:" + date);
|
||||
}
|
||||
int offset = pos.getErrorIndex();
|
||||
if (offset != ((Integer)invalids[k][1]).intValue()) {
|
||||
err = true;
|
||||
System.err.println("\tParse Error [Locale=" +
|
||||
locale + ", " + formats[j] +
|
||||
"/\"" + invalids[k][0] +
|
||||
"\"] incorrect offset. expected:" +
|
||||
invalids[k][1] + ", got: " + offset);
|
||||
} else {
|
||||
/*
|
||||
System.out.println("\tParse Okay [Locale=" +
|
||||
locale + ", " + formats[j] +
|
||||
"/\"" + invalids[k][0] +
|
||||
"\"] correct offset: " + offset);
|
||||
*/
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
err = true;
|
||||
System.err.println("\tParse Error [Locale=" +
|
||||
locale + ", " + formats[j] +
|
||||
"/\"" + invalids[k][0] +
|
||||
"\"] Unexpected Exception occurred: " + e);
|
||||
}
|
||||
}
|
||||
|
||||
/* Error case 2
|
||||
* using DateFormat.parse(String)
|
||||
*/
|
||||
boolean correctParseException = false;
|
||||
for (int k=0; k < invalids.length; k++) {
|
||||
try {
|
||||
date = sdf.parse((String)invalids[k][0]);
|
||||
}
|
||||
catch (ParseException e) {
|
||||
correctParseException = true;
|
||||
int offset = e.getErrorOffset();
|
||||
if (offset != ((Integer)invalids[k][1]).intValue()) {
|
||||
err = true;
|
||||
System.err.println("\tParse Error [Locale=" +
|
||||
locale + ", " + formats[j] +
|
||||
"/\"" + invalids[k][0] +
|
||||
"\"] Expected exception occurred with an incorrect offset. expected:" +
|
||||
invalids[k][1] + ", got: " + offset);
|
||||
} else {
|
||||
/*
|
||||
System.out.println("\tParse Okay [Locale=" +
|
||||
locale + ", " + formats[j] +
|
||||
"/\"" + invalids[k][0] +
|
||||
"\"] Expected exception occurred with an correct offset: "
|
||||
+ offset);
|
||||
*/
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
err = true;
|
||||
System.err.println("\tParse Error [Locale=" +
|
||||
locale + ", " + formats[j] +
|
||||
"/\"" + invalids[k][0] +
|
||||
"\"] Invalid exception occurred: " + e);
|
||||
}
|
||||
finally {
|
||||
if (!correctParseException) {
|
||||
err = true;
|
||||
System.err.println("\tParse Error: [Locale=" +
|
||||
locale + ", " + formats[j] +
|
||||
"/\"" + invalids[k][0] +
|
||||
"\"] Expected exception didn't occur.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
finally {
|
||||
Locale.setDefault(savedLocale);
|
||||
TimeZone.setDefault(savedTimeZone);
|
||||
if (err) {
|
||||
fail("SimpleDateFormat.parse()/format() test failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
78
test/jdk/java/text/Format/DateFormat/Bug4396385.java
Normal file
78
test/jdk/java/text/Format/DateFormat/Bug4396385.java
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
/*
|
||||
* Copyright (c) 2010, 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 4396385
|
||||
* @summary Make sure to detect invalid values for 1-based hour formats.
|
||||
*/
|
||||
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
|
||||
public class Bug4396385 {
|
||||
private static int errorCount = 0;
|
||||
private static String[][] data = {
|
||||
{ "hh:mma", "-1:30AM" },
|
||||
{ "hh:mma", "00:30AM" },
|
||||
{ "hh:mma", "13:30AM" },
|
||||
{ "kk:mm", "-1:12" },
|
||||
{ "kk:mm", "00:12" },
|
||||
{ "kk:mm", "25:12" },
|
||||
};
|
||||
|
||||
public static void main(String[] args) {
|
||||
TimeZone tz = TimeZone.getDefault();
|
||||
// Use "GMT" to avoid any surprises related to offset
|
||||
// transitions.
|
||||
TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
|
||||
|
||||
try {
|
||||
for (String[] item : data) {
|
||||
test(item[0], item[1]);
|
||||
}
|
||||
} finally {
|
||||
// Restore the default time zone
|
||||
TimeZone.setDefault(tz);
|
||||
}
|
||||
|
||||
if (errorCount > 0) {
|
||||
throw new RuntimeException("Failed with " + errorCount + " error(s).");
|
||||
}
|
||||
}
|
||||
|
||||
private static void test(String pattern, String src) {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.US);
|
||||
sdf.setLenient(false);
|
||||
ParsePosition pos = new ParsePosition(0);
|
||||
System.out.printf("parse: \"%s\" with \"%s\"", src, pattern);
|
||||
Date date = sdf.parse(src, pos);
|
||||
System.out.printf(": date = %s, errorIndex = %d", date, pos.getErrorIndex());
|
||||
if (date != null || pos.getErrorIndex() == -1) {
|
||||
System.out.println(": failed");
|
||||
errorCount++;
|
||||
} else {
|
||||
System.out.println(": passed");
|
||||
}
|
||||
}
|
||||
}
|
||||
129
test/jdk/java/text/Format/DateFormat/Bug4407042.java
Normal file
129
test/jdk/java/text/Format/DateFormat/Bug4407042.java
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
/*
|
||||
* Copyright (c) 2001, 2025, 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 4407042
|
||||
* @summary Make sure that cloned SimpleDateFormat objects work
|
||||
* independently in multiple threads.
|
||||
* @library /java/text/testlib
|
||||
* @run junit Bug4407042
|
||||
*/
|
||||
|
||||
import org.junit.jupiter.api.Assumptions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
|
||||
public class Bug4407042 {
|
||||
|
||||
static final String TIME_STRING = "2000/11/18 00:01:00";
|
||||
static final long UTC_LONG = 974534460000L;
|
||||
static SimpleDateFormat masterFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
|
||||
static boolean runrun = true;
|
||||
static int duration = 10;
|
||||
|
||||
@Test
|
||||
void test() {
|
||||
Locale locale = Locale.getDefault();
|
||||
Assumptions.assumeTrue(TestUtils.usesAsciiDigits(locale),
|
||||
locale + " does not use ASCII digits");
|
||||
Assumptions.assumeTrue(TestUtils.usesGregorianCalendar(locale),
|
||||
locale + " does not use a Gregorian calendar");
|
||||
|
||||
masterFormat.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
|
||||
DateParseThread d1 = new DateParseThread();
|
||||
DateFormatThread d2 = new DateFormatThread();
|
||||
d1.start();
|
||||
d2.start();
|
||||
int n = Thread.activeCount();
|
||||
boolean failed = false;
|
||||
|
||||
for (int i = 0; i < duration; i++) {
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
if (Thread.activeCount() != n) {
|
||||
failed = true;
|
||||
break;
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
}
|
||||
runrun = false;
|
||||
try {
|
||||
d1.join();
|
||||
d2.join();
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
if (failed) {
|
||||
throw new RuntimeException("Failed");
|
||||
}
|
||||
}
|
||||
|
||||
synchronized static SimpleDateFormat getFormatter() {
|
||||
return (SimpleDateFormat) masterFormat.clone();
|
||||
}
|
||||
|
||||
static class DateParseThread extends Thread {
|
||||
public void run() {
|
||||
SimpleDateFormat sdf = getFormatter();
|
||||
Calendar cal = null;
|
||||
|
||||
try {
|
||||
int i = 0;
|
||||
while (runrun) {
|
||||
Date date =sdf.parse(TIME_STRING);
|
||||
long t = date.getTime();
|
||||
i++;
|
||||
if (t != UTC_LONG) {
|
||||
throw new RuntimeException("Parse Error: " + i +
|
||||
" (" + sdf.format(date) + ") " + t +
|
||||
" != " + UTC_LONG);
|
||||
}
|
||||
}
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException("Parse Error");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static class DateFormatThread extends Thread {
|
||||
public void run () {
|
||||
SimpleDateFormat sdf = getFormatter();
|
||||
Calendar cal = null;
|
||||
|
||||
int i = 0;
|
||||
while (runrun) {
|
||||
i++;
|
||||
String s = sdf.format(new Date(UTC_LONG));
|
||||
if (!s.equals(TIME_STRING)) {
|
||||
throw new RuntimeException("Format Error: " + i + " " +
|
||||
s + " != " + TIME_STRING);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
54
test/jdk/java/text/Format/DateFormat/Bug4736959.java
Normal file
54
test/jdk/java/text/Format/DateFormat/Bug4736959.java
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
/*
|
||||
* Copyright (c) 2002, 2016, 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 4736959
|
||||
* @summary Make sure to parse "PM" (only) and produce the correct value.
|
||||
*/
|
||||
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public class Bug4736959 {
|
||||
/**
|
||||
* 4736959: JSpinner won't work for AM/PM field
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
SimpleDateFormat f = new SimpleDateFormat("a", Locale.US);
|
||||
|
||||
Date d1 = f.parse("AM", new ParsePosition(0));
|
||||
System.out.println("d1: " + d1);
|
||||
if (d1.getHours() != 0) {
|
||||
throw new RuntimeException("Parsing \"AM\": expected 0 (midnight), got " +
|
||||
d1.getHours());
|
||||
}
|
||||
Date d2 = f.parse("PM", new ParsePosition(0));
|
||||
System.out.println("d2: " + d2);
|
||||
if (d2.getHours() != 12) {
|
||||
throw new RuntimeException("Parsing \"PM\": expected 12 (noon), got " +
|
||||
d2.getHours());
|
||||
}
|
||||
}
|
||||
}
|
||||
76
test/jdk/java/text/Format/DateFormat/Bug4845901.java
Normal file
76
test/jdk/java/text/Format/DateFormat/Bug4845901.java
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
/*
|
||||
* Copyright (c) 2004, 2025, 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 4845901
|
||||
* @summary Make sure that SimpleDateFormat.parse() can distinguish
|
||||
* the same time zone abbreviation for standard and daylight saving
|
||||
* time.
|
||||
* @library /java/text/testlib
|
||||
* @run junit/othervm Bug4845901
|
||||
*/
|
||||
|
||||
import org.junit.jupiter.api.Assumptions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.*;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
public class Bug4845901 {
|
||||
|
||||
@Test
|
||||
void test() {
|
||||
Locale locale = Locale.getDefault();
|
||||
Assumptions.assumeTrue(TestUtils.usesGregorianCalendar(locale),
|
||||
locale + " does not use a Gregorian calendar");
|
||||
|
||||
TimeZone savedTZ = TimeZone.getDefault();
|
||||
TimeZone.setDefault(TimeZone.getTimeZone("Australia/Sydney"));
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss.SSS z");
|
||||
try {
|
||||
testParse(sdf, "2003.01.13 11:10:00.802 AEDT", 11);
|
||||
testParse(sdf, "2003.06.12 11:10:00.802 AEST", 11);
|
||||
testParse(sdf, "2004.12.24 10:10:00.002 AEDT", 10);
|
||||
testParse(sdf, "2004.08.10 10:10:00.002 AEST", 10);
|
||||
} finally {
|
||||
TimeZone.setDefault(savedTZ);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
static void testParse(SimpleDateFormat sdf, String str, int expectedHour) {
|
||||
try {
|
||||
Date parsedDate = sdf.parse(str);
|
||||
if (parsedDate.getHours() != expectedHour) {
|
||||
throw new RuntimeException(
|
||||
"parsed date has wrong hour: " + parsedDate.getHours()
|
||||
+ ", expected: " + expectedHour
|
||||
+ "\ngiven string: " + str
|
||||
+ "\nparsedDate = " + parsedDate);
|
||||
}
|
||||
} catch (java.text.ParseException e) {
|
||||
throw new RuntimeException("parse exception", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
45
test/jdk/java/text/Format/DateFormat/Bug6251817.java
Normal file
45
test/jdk/java/text/Format/DateFormat/Bug6251817.java
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
/*
|
||||
* Copyright (c) 2005, 2016, 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 6251817
|
||||
* @summary Confirm that a typo was corrected.
|
||||
*/
|
||||
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
|
||||
public class Bug6251817 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("zzzz", Locale.US);
|
||||
sdf.setTimeZone(TimeZone.getTimeZone("Australia/Lord_Howe"));
|
||||
String got = sdf.format(new Date());
|
||||
|
||||
if (!got.equals("Lord Howe Standard Time") &&
|
||||
!got.equals("Lord Howe Daylight Time")) {
|
||||
throw new RuntimeException("Timezone display name for Australia/Lord_Howe is incorrect. Got:" + got);
|
||||
}
|
||||
}
|
||||
}
|
||||
163
test/jdk/java/text/Format/DateFormat/Bug6335238.java
Normal file
163
test/jdk/java/text/Format/DateFormat/Bug6335238.java
Normal file
|
|
@ -0,0 +1,163 @@
|
|||
/*
|
||||
* Copyright (c) 2006, 2016, 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 1.1 06/01/24
|
||||
* @bug 6335238
|
||||
* @summary Make sure that both the original and cloned SimpleDateFormat coexistindependently and don't cut off each other.
|
||||
* @run main Bug6335238 10
|
||||
*/
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
|
||||
// Usage: java Bug6335238 [duration]
|
||||
public class Bug6335238 {
|
||||
|
||||
static final long UTC_LONG = 974534460000L;
|
||||
static final String TIME_STRING = "2000/11/18 00:01:00";
|
||||
static SimpleDateFormat masterSdf;
|
||||
static int duration = 180;
|
||||
static boolean stopped = false;
|
||||
static boolean err = false;
|
||||
|
||||
public static void main(String[] args) {
|
||||
if (args.length == 1) {
|
||||
duration = Math.max(10, Integer.parseInt(args[0]));
|
||||
}
|
||||
Locale savedLocale = Locale.getDefault();
|
||||
TimeZone savedTimeZone = TimeZone.getDefault();
|
||||
|
||||
TimeZone.setDefault(TimeZone.getTimeZone("US/Pacific"));
|
||||
Locale.setDefault(Locale.US);
|
||||
|
||||
masterSdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
|
||||
|
||||
try {
|
||||
// Once it is used, DecimalFormat becomes not thread-safe.
|
||||
Date d = masterSdf.parse(TIME_STRING);
|
||||
|
||||
new Bug6335238();
|
||||
} catch (Exception e) {
|
||||
System.err.println(e);
|
||||
err = true;
|
||||
} finally {
|
||||
TimeZone.setDefault(savedTimeZone);
|
||||
Locale.setDefault(savedLocale);
|
||||
|
||||
if (err) {
|
||||
throw new RuntimeException("Failed: Multiple DateFormat instances didn't work correctly.");
|
||||
} else {
|
||||
System.out.println("Passed.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Bug6335238() {
|
||||
stopped = false;
|
||||
|
||||
DateParseThread d1 = new DateParseThread();
|
||||
DateFormatThread d2 = new DateFormatThread();
|
||||
DateParseThread d3 = new DateParseThread();
|
||||
DateFormatThread d4 = new DateFormatThread();
|
||||
|
||||
d1.start();
|
||||
d2.start();
|
||||
d3.start();
|
||||
d4.start();
|
||||
|
||||
try {
|
||||
Thread.sleep(duration * 1000);
|
||||
}
|
||||
catch (Exception e) {
|
||||
System.err.println(e);
|
||||
err = true;
|
||||
}
|
||||
|
||||
stopped = true;
|
||||
}
|
||||
|
||||
class DateFormatThread extends Thread {
|
||||
|
||||
public void run() {
|
||||
int i = 0;
|
||||
|
||||
while (!stopped) {
|
||||
SimpleDateFormat sdf;
|
||||
synchronized (masterSdf) {
|
||||
sdf = (SimpleDateFormat)masterSdf.clone();
|
||||
}
|
||||
|
||||
i++;
|
||||
String s = sdf.format(new Date(UTC_LONG));
|
||||
|
||||
if (!s.equals(TIME_STRING)) {
|
||||
stopped = true;
|
||||
err = true;
|
||||
|
||||
throw new RuntimeException("Formatting Date Error: counter=" +
|
||||
i + ", Got<" + s + "> != Expected<" + TIME_STRING + ">");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class DateParseThread extends Thread {
|
||||
|
||||
public void run() {
|
||||
int i = 0;
|
||||
|
||||
while (!stopped) {
|
||||
SimpleDateFormat sdf;
|
||||
synchronized (masterSdf) {
|
||||
sdf = (SimpleDateFormat)masterSdf.clone();
|
||||
}
|
||||
|
||||
i++;
|
||||
Date date;
|
||||
try {
|
||||
date = sdf.parse(TIME_STRING);
|
||||
long t = date.getTime();
|
||||
|
||||
if (t != UTC_LONG) {
|
||||
stopped = true;
|
||||
err = true;
|
||||
|
||||
throw new RuntimeException("Parsing Date Error: counter=" +
|
||||
i + " Got:" + t + "<" + sdf.format(date) +
|
||||
"> != " + UTC_LONG);
|
||||
}
|
||||
}
|
||||
catch (ParseException e) {
|
||||
stopped = true;
|
||||
err = true;
|
||||
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
97
test/jdk/java/text/Format/DateFormat/Bug6530336.java
Normal file
97
test/jdk/java/text/Format/DateFormat/Bug6530336.java
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
/*
|
||||
* Copyright (c) 2007, 2025, 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 6530336 6537997 8008577 8174269 8333582
|
||||
* @library /java/text/testlib
|
||||
* @run junit/othervm Bug6530336
|
||||
*/
|
||||
|
||||
import org.junit.jupiter.api.Assumptions;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.FieldSource;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class Bug6530336 {
|
||||
|
||||
private static final Locale[] locales = Locale.getAvailableLocales();
|
||||
private static final TimeZone[] timezones = {
|
||||
TimeZone.getTimeZone("America/New_York"),
|
||||
TimeZone.getTimeZone("America/Denver"),
|
||||
};
|
||||
private static final TimeZone timezone_LA = TimeZone.getTimeZone("America/Los_Angeles");
|
||||
private static final String[] expected = {
|
||||
"Sun Jul 15 12:00:00 PDT 2007",
|
||||
"Sun Jul 15 14:00:00 PDT 2007",
|
||||
};
|
||||
private static final Date[] dates = new Date[2];
|
||||
|
||||
@BeforeAll
|
||||
static void setup() {
|
||||
TimeZone.setDefault(timezone_LA);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@FieldSource("locales")
|
||||
void test(Locale locale) {
|
||||
Assumptions.assumeTrue(TestUtils.usesGregorianCalendar(locale),
|
||||
locale + " does not use a Gregorian calendar");
|
||||
Locale.setDefault(locale);
|
||||
|
||||
for (int j = 0; j < timezones.length; j++) {
|
||||
Calendar cal = Calendar.getInstance(timezones[j]);
|
||||
cal.set(2007, 6, 15, 15, 0, 0);
|
||||
dates[j] = cal.getTime();
|
||||
}
|
||||
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
|
||||
|
||||
for (int j = 0; j < timezones.length; j++) {
|
||||
sdf.setTimeZone(timezones[j]);
|
||||
String date = sdf.format(dates[j]);
|
||||
// CLDR localizes GMT format into for some locales. Ignore those cases
|
||||
if (date.matches(".*GMT[\\s+-]\\D.*") ||
|
||||
date.contains("UTC") ||
|
||||
date.contains("TMG") || // Interlingue
|
||||
date.contains("ߜ߭ߕߖ") || // N’Ko
|
||||
date.contains("ꋧꃅꎕꏦꄮꈉ") || // Sichuan Yi, Nuosu
|
||||
date.contains("گرینیچ")) { // Central Kurdish
|
||||
continue;
|
||||
}
|
||||
sdf.setTimeZone(timezone_LA);
|
||||
String date_LA = assertDoesNotThrow(() -> sdf.parse(date).toString());
|
||||
assertEquals(expected[j], date_LA,
|
||||
"Got wrong Pacific time (%s) for (%s) in %s in %s.".formatted(date_LA, date, locale, timezones[j]));
|
||||
}
|
||||
}
|
||||
}
|
||||
82
test/jdk/java/text/Format/DateFormat/Bug6609750.java
Normal file
82
test/jdk/java/text/Format/DateFormat/Bug6609750.java
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
* Copyright (c) 2009, 2016, 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 6609750
|
||||
* @summary Make sure that SimpleDateFormat.format() formats years correctly.
|
||||
*/
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
|
||||
public class Bug6609750 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
boolean error = false;
|
||||
|
||||
Locale defaultLocale = Locale.getDefault();
|
||||
Locale.setDefault(Locale.US);
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
Date[] dates = {
|
||||
new Date(9-1900, Calendar.JUNE, 12),
|
||||
new Date(99-1900, Calendar.JUNE, 12),
|
||||
new Date(999-1900, Calendar.JUNE, 12),
|
||||
new Date(2009-1900, Calendar.JUNE, 12),
|
||||
new Date(30009-1900, Calendar.JUNE, 12),
|
||||
};
|
||||
|
||||
String[] patterns = {
|
||||
"y", "yy", "yyy", "yyyy", "yyyyy", "yyyyyy"
|
||||
};
|
||||
String[][] expectedResults = {
|
||||
{"9", "09", "009", "0009", "00009", "000009"},
|
||||
{"99", "99", "099", "0099", "00099", "000099"},
|
||||
{"999", "99", "999", "0999", "00999", "000999"},
|
||||
{"2009", "09", "2009", "2009", "02009", "002009"},
|
||||
{"30009", "09", "30009", "30009", "30009", "030009"},
|
||||
};
|
||||
|
||||
SimpleDateFormat sdf = new SimpleDateFormat();
|
||||
for (int dateNo = 0; dateNo < dates.length; dateNo++) {
|
||||
Date date = dates[dateNo];
|
||||
for (int patternNo = 0; patternNo < patterns.length; patternNo++) {
|
||||
sdf.applyPattern(patterns[patternNo]);
|
||||
String got = sdf.format(date);
|
||||
if (!expectedResults[dateNo][patternNo].equals(got)) {
|
||||
error = true;
|
||||
System.err.println("Failed: Unexpected format result: " +
|
||||
"Expected: \"" + expectedResults[dateNo][patternNo] +
|
||||
"\", Got: \"" + got + "\" for date " + date +
|
||||
" with pattern \"" + patterns[patternNo] + "\"");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Locale.setDefault(defaultLocale);
|
||||
if (error) {
|
||||
throw new RuntimeException("SimpleDateFormat.format() error.");
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
66
test/jdk/java/text/Format/DateFormat/Bug6645292.java
Normal file
66
test/jdk/java/text/Format/DateFormat/Bug6645292.java
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
* Copyright (c) 2008, 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 6645292
|
||||
* @summary Make sure to parse a DST time zone name with which the
|
||||
* last DST rule doesn't observe DST.
|
||||
*/
|
||||
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import static java.util.Calendar.*;
|
||||
|
||||
public class Bug6645292 {
|
||||
public static void main(String[] args) {
|
||||
Locale loc = Locale.getDefault();
|
||||
TimeZone zone = TimeZone.getDefault();
|
||||
try {
|
||||
Locale.setDefault(Locale.US);
|
||||
// Use "Asia/Shanghai" with an old time stamp rather than
|
||||
// "Australia/Perth" because if Perth decides to obserb DST
|
||||
// permanently, that decision will make this test case
|
||||
// useless. There's the same risk with China, though.
|
||||
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.clear();
|
||||
cal.set(1986, JUNE, 1);
|
||||
Date d1 = cal.getTime();
|
||||
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss zzzz");
|
||||
String s = df.format(d1);
|
||||
Date d2 = null;
|
||||
try {
|
||||
d2 = df.parse(s);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
if (!d1.equals(d2)) {
|
||||
throw new RuntimeException("d1 (" + d1 + ") != d2 (" + d2 + ")");
|
||||
}
|
||||
} finally {
|
||||
Locale.setDefault(loc);
|
||||
TimeZone.setDefault(zone);
|
||||
}
|
||||
}
|
||||
}
|
||||
118
test/jdk/java/text/Format/DateFormat/Bug6683975.java
Normal file
118
test/jdk/java/text/Format/DateFormat/Bug6683975.java
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
/*
|
||||
* Copyright (c) 2008, 2024, 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 6683975 8008577 8174269
|
||||
* @summary Make sure that date is formatted correctly in th locale.
|
||||
* @modules jdk.localedata
|
||||
* @run main Bug6683975
|
||||
*/
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
|
||||
public class Bug6683975 {
|
||||
|
||||
private static boolean err = false;
|
||||
|
||||
private static Locale th = Locale.of("th");
|
||||
private static Locale th_TH = Locale.of("th", "TH");
|
||||
private static String expected_th[] = {
|
||||
"\u0e27\u0e31\u0e19\u0e2d\u0e31\u0e07\u0e04\u0e32\u0e23\u0e17\u0e35\u0e48 30 \u0e01\u0e31\u0e19\u0e22\u0e32\u0e22\u0e19 \u0e04.\u0e28. 2008 8 \u0e19\u0e32\u0e2c\u0e34\u0e01\u0e32 00 \u0e19\u0e32\u0e17\u0e35 00 \u0e27\u0e34\u0e19\u0e32\u0e17\u0e35 \u0e40\u0e27\u0e25\u0e32\u0e2d\u0e2d\u0e21\u0e41\u0e2a\u0e07\u0e41\u0e1b\u0e0b\u0e34\u0e1f\u0e34\u0e01\u0e43\u0e19\u0e2d\u0e40\u0e21\u0e23\u0e34\u0e01\u0e32\u0e40\u0e2b\u0e19\u0e37\u0e2d", // 0: FULL
|
||||
"30 \u0e01\u0e31\u0e19\u0e22\u0e32\u0e22\u0e19 \u0e04.\u0e28. 2008 8 \u0e19\u0e32\u0e2c\u0e34\u0e01\u0e32 00 \u0e19\u0e32\u0e17\u0e35 00 \u0e27\u0e34\u0e19\u0e32\u0e17\u0e35 PDT", // 1: LONG
|
||||
"30 \u0e01.\u0e22. 2008 08:00:00", // 2: MEDIUM
|
||||
"30/9/08 08:00", // 3: SHORT
|
||||
};
|
||||
private static String expected_th_TH[] = {
|
||||
"\u0e27\u0e31\u0e19\u0e2d\u0e31\u0e07\u0e04\u0e32\u0e23\u0e17\u0e35\u0e48 30 \u0e01\u0e31\u0e19\u0e22\u0e32\u0e22\u0e19 \u0e1e\u0e38\u0e17\u0e18\u0e28\u0e31\u0e01\u0e23\u0e32\u0e0a 2551 8 \u0e19\u0e32\u0e2c\u0e34\u0e01\u0e32 00 \u0e19\u0e32\u0e17\u0e35 00 \u0e27\u0e34\u0e19\u0e32\u0e17\u0e35 \u0e40\u0e27\u0e25\u0e32\u0e2d\u0e2d\u0e21\u0e41\u0e2a\u0e07\u0e41\u0e1b\u0e0b\u0e34\u0e1f\u0e34\u0e01\u0e43\u0e19\u0e2d\u0e40\u0e21\u0e23\u0e34\u0e01\u0e32\u0e40\u0e2b\u0e19\u0e37\u0e2d", // 0: FULL
|
||||
"30 \u0e01\u0e31\u0e19\u0e22\u0e32\u0e22\u0e19 2551 8 \u0e19\u0e32\u0e2c\u0e34\u0e01\u0e32 00 \u0e19\u0e32\u0e17\u0e35 00 \u0e27\u0e34\u0e19\u0e32\u0e17\u0e35 PDT", // 1: LONG
|
||||
"30 \u0e01.\u0e22. 2551 08:00:00", // 2: MEDIUM
|
||||
"30/9/51 08:00" // 3: SHORT
|
||||
};
|
||||
private static String stylePattern[] = {
|
||||
"FULL", "LONG", "MEDIUM", "SHORT"
|
||||
};
|
||||
|
||||
private static void test(int style) {
|
||||
DateFormat df_th = DateFormat.getDateTimeInstance(style, style, th);
|
||||
DateFormat df_th_TH = DateFormat.getDateTimeInstance(style, style, th_TH);
|
||||
|
||||
String str_th = ((SimpleDateFormat)df_th).toPattern();
|
||||
String str_th_TH = ((SimpleDateFormat)df_th_TH).toPattern();
|
||||
// CLDR has different patterns for Gregorian and Buddhist calendars, thus they don't match
|
||||
// if (!str_th.equals(str_th_TH)) {
|
||||
// err = true;
|
||||
// System.err.println("Error: Pattern for th locale should be the same as pattern for th_TH locale. (" + stylePattern[style] + ")");
|
||||
// System.err.println("\tth: " + str_th);
|
||||
// System.err.println("\tth_TH: " + str_th_TH);
|
||||
// }
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
Date date = new Date(2008-1900, Calendar.SEPTEMBER, 30, 8, 0, 0);
|
||||
str_th = df_th.format(date);
|
||||
if (!expected_th[style].equals(str_th)) {
|
||||
err = true;
|
||||
System.err.println("Error: Formatted date in th locale is incorrect in " + stylePattern[style] + " pattern.");
|
||||
System.err.println("\tExpected: " + expected_th[style]);
|
||||
System.err.println("\tGot: " + str_th);
|
||||
}
|
||||
|
||||
str_th_TH = df_th_TH.format(date);
|
||||
if (!expected_th_TH[style].equals(str_th_TH)) {
|
||||
err = true;
|
||||
System.err.println("Error: Formatted date in th_TH locale is incorrect in " + stylePattern[style] + " pattern.");
|
||||
System.err.println("\tExpected: " + expected_th_TH[style]);
|
||||
System.err.println("\tGot: " + str_th_TH);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
TimeZone timezone = TimeZone.getDefault();
|
||||
Locale locale = Locale.getDefault();
|
||||
|
||||
TimeZone.setDefault(TimeZone.getTimeZone("US/Pacific"));
|
||||
Locale.setDefault(Locale.US);
|
||||
|
||||
try {
|
||||
test(DateFormat.FULL);
|
||||
test(DateFormat.LONG);
|
||||
test(DateFormat.MEDIUM);
|
||||
test(DateFormat.SHORT);
|
||||
}
|
||||
catch (Exception e) {
|
||||
err = true;
|
||||
System.err.println("Unexpected exception was thrown: " + e);
|
||||
}
|
||||
finally {
|
||||
TimeZone.setDefault(timezone);
|
||||
Locale.setDefault(locale);
|
||||
|
||||
if (err) {
|
||||
throw new RuntimeException("Failed.");
|
||||
} else {
|
||||
System.out.println("Passed.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
94
test/jdk/java/text/Format/DateFormat/Bug7130335.java
Normal file
94
test/jdk/java/text/Format/DateFormat/Bug7130335.java
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
/*
|
||||
* Copyright (c) 2012, 2013, 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 7130335 7130335
|
||||
* @summary Make sure that round-trip conversion (format/parse) works
|
||||
* with old timestamps in Europe/Moscow and with multiple time zone letters.
|
||||
*/
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import static java.util.GregorianCalendar.*;
|
||||
|
||||
public class Bug7130335 {
|
||||
private static final TimeZone MOSCOW = TimeZone.getTimeZone("Europe/Moscow");
|
||||
private static final TimeZone LONDON = TimeZone.getTimeZone("Europe/London");
|
||||
private static final TimeZone LA = TimeZone.getTimeZone("America/Los_Angeles");
|
||||
private static final TimeZone[] ZONES = {
|
||||
MOSCOW, LONDON, LA
|
||||
};
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z", Locale.US);
|
||||
sdf.setTimeZone(MOSCOW);
|
||||
Calendar cal = new GregorianCalendar(MOSCOW, Locale.US);
|
||||
cal.clear();
|
||||
// Try both +03:00 and +02:00
|
||||
cal.set(1922, SEPTEMBER, 30);
|
||||
test(sdf, cal);
|
||||
cal.add(DAY_OF_YEAR, 1);
|
||||
test(sdf, cal);
|
||||
cal.set(1991, MARCH, 31);
|
||||
// in daylight saving time
|
||||
test(sdf, cal);
|
||||
cal.add(DAY_OF_YEAR, 1);
|
||||
test(sdf, cal);
|
||||
// Try the current timestamp
|
||||
cal.setTimeInMillis(System.currentTimeMillis());
|
||||
test(sdf, cal);
|
||||
|
||||
// tests for multiple time zone letters (8000529)
|
||||
test8000529("yyyy-MM-dd HH:mm:ss.SSS Z (z)");
|
||||
test8000529("yyyy-MM-dd HH:mm:ss.SSS Z (zzzz)");
|
||||
test8000529("yyyy-MM-dd HH:mm:ss.SSS z (Z)");
|
||||
test8000529("yyyy-MM-dd HH:mm:ss.SSS zzzz (Z)");
|
||||
|
||||
}
|
||||
|
||||
private static void test(SimpleDateFormat sdf, Calendar cal) throws Exception {
|
||||
Date d = cal.getTime();
|
||||
String f = sdf.format(d);
|
||||
System.out.println(f);
|
||||
Date pd = sdf.parse(f);
|
||||
String p = sdf.format(pd);
|
||||
if (!d.equals(pd) || !f.equals(p)) {
|
||||
throw new RuntimeException("format: " + f + ", parse: " + p);
|
||||
}
|
||||
}
|
||||
|
||||
private static void test8000529(String fmt) throws Exception {
|
||||
for (TimeZone tz : ZONES) {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(fmt, Locale.US);
|
||||
sdf.setTimeZone(tz);
|
||||
Calendar cal = new GregorianCalendar(tz, Locale.US);
|
||||
cal.clear();
|
||||
cal.set(2012, JUNE, 22);
|
||||
test(sdf, cal);
|
||||
cal.set(2012, DECEMBER, 22);
|
||||
test(sdf, cal);
|
||||
cal.setTimeInMillis(System.currentTimeMillis());
|
||||
test(sdf, cal);
|
||||
}
|
||||
}
|
||||
}
|
||||
58
test/jdk/java/text/Format/DateFormat/Bug7177315.java
Normal file
58
test/jdk/java/text/Format/DateFormat/Bug7177315.java
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
/*
|
||||
* Copyright (c) 2013, 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 7177315
|
||||
* @summary Make sure that space characters are properly skipped when
|
||||
* parsing 2-digit year values.
|
||||
*/
|
||||
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
|
||||
public class Bug7177315 {
|
||||
private static final String EXPECTED = "01/01/2012";
|
||||
private static final String[] DATA = {
|
||||
"01/01/12",
|
||||
"01/01/ 12",
|
||||
"01/01/ 12",
|
||||
"1/1/12",
|
||||
"1/1/ 12"
|
||||
};
|
||||
|
||||
public static void main (String[] args) throws ParseException {
|
||||
SimpleDateFormat parseFormat = new SimpleDateFormat("MM/dd/yy", Locale.US);
|
||||
Calendar cal = new GregorianCalendar(2012-80, Calendar.JANUARY, 1);
|
||||
parseFormat.set2DigitYearStart(cal.getTime());
|
||||
SimpleDateFormat fmtFormat = new SimpleDateFormat("MM/dd/yyyy", Locale.US);
|
||||
|
||||
for (String text : DATA) {
|
||||
Date date = parseFormat.parse(text);
|
||||
String got = fmtFormat.format(date);
|
||||
if (!EXPECTED.equals(got)) {
|
||||
throw new RuntimeException("got: " + got + ", expected: " + EXPECTED);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
70
test/jdk/java/text/Format/DateFormat/Bug8072099.java
Normal file
70
test/jdk/java/text/Format/DateFormat/Bug8072099.java
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
/*
|
||||
* Copyright (c) 2016, 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 8072099
|
||||
* @summary check the date time pattern for <NUMERIC_FIELD> which should
|
||||
* not throw ParseException
|
||||
*/
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Locale;
|
||||
|
||||
public class Bug8072099 {
|
||||
|
||||
private static String[][] shouldPass = {
|
||||
{"ha", "11AM"},
|
||||
{"hma", "33AM"},
|
||||
{"ka", "24AM"},
|
||||
{"yyyMMM", "2016May"},
|
||||
{"yyyyDDEEE", "2016366Sat"},
|
||||
{"ddmyyyyz", "22111980GMT+5:30"}
|
||||
};
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Locale defaultLocale = Locale.getDefault();
|
||||
try {
|
||||
Locale.setDefault(Locale.US);
|
||||
// check the date time pattern which should pass
|
||||
for (String[] pattern : shouldPass) {
|
||||
SimpleDateFormat dateTimeFormat = new SimpleDateFormat(pattern[0]);
|
||||
parseDateTimeInput(dateTimeFormat, pattern[1]);
|
||||
}
|
||||
} finally {
|
||||
Locale.setDefault(defaultLocale);
|
||||
}
|
||||
}
|
||||
|
||||
private static void parseDateTimeInput(SimpleDateFormat format,
|
||||
String inputString) {
|
||||
try {
|
||||
format.parse(inputString);
|
||||
} catch (ParseException ex) {
|
||||
throw new RuntimeException("[FAILED: Unable to parse date time"
|
||||
+ " string " + inputString + "]");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
51
test/jdk/java/text/Format/DateFormat/Bug8081794.java
Normal file
51
test/jdk/java/text/Format/DateFormat/Bug8081794.java
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
* Copyright (c) 2015, 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 8081794
|
||||
* @summary ParsePosition getErrorIndex should return correct index
|
||||
*/
|
||||
import java.text.ParsePosition;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
public class Bug8081794 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String date = "13 Jan 2005 21:45:34 ABC";
|
||||
String format = "dd MMM yyyy HH:mm:ss z";
|
||||
ParsePosition pp = new ParsePosition(0);
|
||||
pp.setIndex(0);
|
||||
SimpleDateFormat sd = new SimpleDateFormat(format, Locale.ENGLISH);
|
||||
Date d = sd.parse(date, pp);
|
||||
int errorIndex = pp.getErrorIndex();
|
||||
if (errorIndex == 21) {
|
||||
System.out.println(": passed");
|
||||
} else {
|
||||
System.out.println(": failed");
|
||||
throw new RuntimeException("Failed with wrong index: " + errorIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
121
test/jdk/java/text/Format/DateFormat/Bug8139572.java
Normal file
121
test/jdk/java/text/Format/DateFormat/Bug8139572.java
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
/*
|
||||
* Copyright (c) 2015, 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 8139572
|
||||
* @summary SimpleDateFormat parse month stand-alone format bug
|
||||
* @compile -encoding utf-8 Bug8139572.java
|
||||
* @modules jdk.localedata
|
||||
* @run main Bug8139572
|
||||
*/
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.Locale;
|
||||
|
||||
public class Bug8139572 {
|
||||
|
||||
private static final Locale RUSSIAN = Locale.of("ru");
|
||||
private static final Date SEPT12 = new GregorianCalendar(2015, Calendar.SEPTEMBER, 12).getTime();
|
||||
|
||||
private static final String[] PATTERNS = {
|
||||
"L",
|
||||
"dd L",
|
||||
"dd L yy",
|
||||
"dd L yyyy",
|
||||
"LL",
|
||||
"dd LL",
|
||||
"dd LL yy",
|
||||
"dd LL yyyy",
|
||||
"LLL",
|
||||
"dd LLL",
|
||||
"dd LLL yy",
|
||||
"dd LLL yyyy",
|
||||
"LLLL",
|
||||
"dd LLLL",
|
||||
"dd LLLL yy",
|
||||
"dd LLLL yyyy"
|
||||
};
|
||||
|
||||
private static final String[] APPLIED = {
|
||||
"9",
|
||||
"12 09",
|
||||
"12 09 15",
|
||||
"12 09 2015",
|
||||
"09",
|
||||
"12 09",
|
||||
"12 09 15",
|
||||
"12 09 2015",
|
||||
"сентября",
|
||||
"12 сентября",
|
||||
"12 сентября 15",
|
||||
"12 сентября 2015",
|
||||
"сентября",
|
||||
"12 сентября",
|
||||
"12 сентября 15",
|
||||
"12 сентября 2015"
|
||||
};
|
||||
|
||||
private static final String[] EXPECTED = {
|
||||
"9",
|
||||
"12 9",
|
||||
"12 9 15",
|
||||
"12 9 2015",
|
||||
"09",
|
||||
"12 09",
|
||||
"12 09 15",
|
||||
"12 09 2015",
|
||||
"сент.",
|
||||
"12 сент.",
|
||||
"12 сент. 15",
|
||||
"12 сент. 2015",
|
||||
"сентябрь",
|
||||
"12 сентябрь",
|
||||
"12 сентябрь 15",
|
||||
"12 сентябрь 2015"
|
||||
};
|
||||
|
||||
public static void main(String[] args) throws ParseException {
|
||||
|
||||
for (int i = 0; i < PATTERNS.length; i++) {
|
||||
SimpleDateFormat fmt = new SimpleDateFormat(PATTERNS[i], RUSSIAN);
|
||||
Date standAloneDate = fmt.parse(APPLIED[i]);
|
||||
String str = fmt.format(standAloneDate);
|
||||
if (!EXPECTED[i].equals(str)) {
|
||||
throw new RuntimeException("bad result: got '" + str + "', expected '" + EXPECTED[i] + "'");
|
||||
}
|
||||
}
|
||||
|
||||
SimpleDateFormat fmt = new SimpleDateFormat("", RUSSIAN);
|
||||
for (int j = 0; j < PATTERNS.length; j++) {
|
||||
fmt.applyPattern(PATTERNS[j]);
|
||||
String str = fmt.format(SEPT12);
|
||||
if (!EXPECTED[j].equals(str)) {
|
||||
throw new RuntimeException("bad result: got '" + str + "', expected '" + EXPECTED[j] + "'");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
83
test/jdk/java/text/Format/DateFormat/Bug8141243.java
Normal file
83
test/jdk/java/text/Format/DateFormat/Bug8141243.java
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
/*
|
||||
* Copyright (c) 2015, 2024, 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 8141243 8174269
|
||||
* @summary Make sure that SimpleDateFormat parses "UTC" as the UTC time zone.
|
||||
* @run main Bug8141243
|
||||
*/
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
import static java.util.TimeZone.*;
|
||||
|
||||
public class Bug8141243 {
|
||||
public static void main(String[] args) {
|
||||
TimeZone UTC = TimeZone.getTimeZone("UTC");
|
||||
TimeZone initTz = TimeZone.getDefault();
|
||||
|
||||
List<String> errors = new ArrayList<>();
|
||||
try {
|
||||
TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
|
||||
for (Locale locale : DateFormat.getAvailableLocales()) {
|
||||
// exclude any locales which localize "UTC".
|
||||
String utc = UTC.getDisplayName(false, SHORT, locale);
|
||||
if (!"UTC".equals(utc)) {
|
||||
System.out.println("Skipping " + locale + " due to localized UTC name: " + utc);
|
||||
continue;
|
||||
}
|
||||
SimpleDateFormat fmt = new SimpleDateFormat("z", locale);
|
||||
try {
|
||||
Date date = fmt.parse("UTC");
|
||||
// Parsed one may not exactly be UTC. Universal, UCT, etc. are equivalents.
|
||||
if (!fmt.getTimeZone().getID().matches("(Etc/)?(UTC|Universal|UCT|Zulu)")) {
|
||||
errors.add("timezone: " + fmt.getTimeZone().getID()
|
||||
+ ", locale: " + locale);
|
||||
}
|
||||
} catch (ParseException e) {
|
||||
errors.add("parse exception: " + e + ", locale: " + locale);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
// Restore the default time zone
|
||||
TimeZone.setDefault(initTz);
|
||||
}
|
||||
|
||||
if (!errors.isEmpty()) {
|
||||
System.out.println("Got unexpected results:");
|
||||
for (String s : errors) {
|
||||
System.out.println(" " + s);
|
||||
}
|
||||
throw new RuntimeException("Test failed.");
|
||||
} else {
|
||||
System.out.println("Test passed.");
|
||||
}
|
||||
}
|
||||
}
|
||||
88
test/jdk/java/text/Format/DateFormat/Bug8193444.java
Normal file
88
test/jdk/java/text/Format/DateFormat/Bug8193444.java
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
/*
|
||||
* Copyright (c) 2018, 2025, 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 8193444
|
||||
* @summary Checks SimpleDateFormat.format/parse for the AIOOB exception when
|
||||
* formatting/parsing dates through a pattern string that contains a
|
||||
* sequence of 256 or more non-ASCII unicode characters.
|
||||
* @run junit/othervm Bug8193444
|
||||
*/
|
||||
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
public class Bug8193444 {
|
||||
|
||||
private static final String NON_ASCII_CHAR = "\u263A";
|
||||
|
||||
Object[][] dateFormatData() {
|
||||
return new Object[][]{
|
||||
// short_length (between 0 and 254)
|
||||
{250},
|
||||
// boundary
|
||||
{254},
|
||||
// long_length
|
||||
{257},};
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("dateFormatData")
|
||||
void testDateFormatAndParse(int length)
|
||||
throws ParseException {
|
||||
|
||||
String pattern = NON_ASCII_CHAR.repeat(length);
|
||||
|
||||
DateFormat df = new SimpleDateFormat(pattern);
|
||||
// format() should not throw AIOOB exception
|
||||
String result = df.format(new Date());
|
||||
|
||||
// Since the tested format patterns do not contain any character
|
||||
// representing date/time field, those characters are not interpreted,
|
||||
// they are simply copied into the output string during formatting
|
||||
assertEquals(pattern, result, "Failed to format the date using"
|
||||
+ " pattern of length: " + length);
|
||||
|
||||
// The format pattern used by this SimpleDateFormat
|
||||
// contains a sequence of non-ASCII characters, which does not
|
||||
// represent any date/time field. The same sequence is given
|
||||
// for parsing, just to check that the parsing does
|
||||
// not throw any AIOOB exception.
|
||||
// Although as per the parse() specification, the calendar's default
|
||||
// values of the date-time fields are used for any missing
|
||||
// date-time information, but checking that is not the intention of
|
||||
// this test.
|
||||
df.parse(pattern);
|
||||
}
|
||||
|
||||
}
|
||||
50
test/jdk/java/text/Format/DateFormat/Bug8209047.java
Normal file
50
test/jdk/java/text/Format/DateFormat/Bug8209047.java
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
* Copyright (c) 2018, 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 8209047
|
||||
* @modules jdk.localedata
|
||||
* @summary Check that DateFormat's getDateInstance, getTimeInstance and getDateTimeInstance works with all locales.
|
||||
*/
|
||||
import java.text.DateFormat;
|
||||
import java.util.Arrays;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
|
||||
public class Bug8209047 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Set<Integer> styles = Set.of(DateFormat.FULL, DateFormat.LONG, DateFormat.MEDIUM, DateFormat.SHORT);
|
||||
Arrays.stream(Locale.getAvailableLocales()).forEach(locale -> {
|
||||
styles.forEach(style -> {
|
||||
try {
|
||||
DateFormat.getDateInstance(style, locale);
|
||||
DateFormat.getTimeInstance(style, locale);
|
||||
DateFormat.getDateTimeInstance(style, style, locale);
|
||||
} catch (IllegalArgumentException ex) {
|
||||
throw new RuntimeException("Getting DateFormat instance failed for locale " + locale, ex);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
37
test/jdk/java/text/Format/DateFormat/Bug8235699.java
Normal file
37
test/jdk/java/text/Format/DateFormat/Bug8235699.java
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
* Copyright Amazon.com Inc. 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 8235699
|
||||
* @summary Check that CalendarBuilder.toString() does not throw an ArrayIndexOutOfBoundException
|
||||
* @build java.base/java.text.CalendarBuilderTest
|
||||
* @run main Bug8235699
|
||||
*/
|
||||
|
||||
|
||||
public class Bug8235699 {
|
||||
public static void main(String[] args) {
|
||||
java.text.CalendarBuilderTest.testCalendarBuilderToString();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,76 @@
|
|||
/*
|
||||
* Copyright (c) 2020, 2025, 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 8248434
|
||||
* @modules jdk.localedata
|
||||
* @summary Checks format/parse round trip in case-insensitive manner.
|
||||
* @run junit/othervm CaseInsensitiveParseTest
|
||||
*/
|
||||
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
public class CaseInsensitiveParseTest {
|
||||
|
||||
private final static String PATTERN = "GGGG/yyyy/MMMM/dddd/hhhh/mmmm/ss/aaaa";
|
||||
private final static Date EPOCH = new Date(0L);
|
||||
|
||||
Object[][] locales() {
|
||||
return (Object[][])Arrays.stream(DateFormat.getAvailableLocales())
|
||||
.map(Stream::of)
|
||||
.map(Stream::toArray)
|
||||
.toArray(Object[][]::new);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("locales")
|
||||
void testUpperCase(Locale loc) throws ParseException {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(PATTERN, loc);
|
||||
String formatted = sdf.format(EPOCH);
|
||||
assertEquals(EPOCH, sdf.parse(formatted.toUpperCase(Locale.ROOT)),
|
||||
"roundtrip failed for string '" + formatted + "', locale: " + loc);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("locales")
|
||||
void testLowerCase(Locale loc) throws ParseException {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(PATTERN, loc);
|
||||
String formatted = sdf.format(EPOCH);
|
||||
assertEquals(EPOCH, sdf.parse(formatted.toLowerCase(Locale.ROOT)),
|
||||
"roundtrip failed for string '" + formatted + "', locale: " + loc);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,68 @@
|
|||
/*
|
||||
* Copyright (c) 2012, 2024, 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 7079560 8008577 8174269
|
||||
* @summary Unit test for context-sensitive month names
|
||||
* @modules jdk.localedata
|
||||
* @run main ContextMonthNamesTest
|
||||
*/
|
||||
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
|
||||
public class ContextMonthNamesTest {
|
||||
static Locale CZECH = Locale.of("cs");
|
||||
static Date JAN30 = new GregorianCalendar(2012, Calendar.JANUARY, 30).getTime();
|
||||
|
||||
static String[] PATTERNS = {
|
||||
"d. MMMM yyyy", // format
|
||||
"d. MMM yyyy", // format (abbr)
|
||||
"MMMM", // stand-alone
|
||||
"MMM", // stand-alone (abbr)
|
||||
"d. LLLL yyyy", // force stand-alone
|
||||
"d. LLL yyyy", // force stand-alone (abbr)
|
||||
};
|
||||
// NOTE: expected results are locale data dependent.
|
||||
static String[] EXPECTED = {
|
||||
"30. ledna 2012",
|
||||
"30. led 2012",
|
||||
"leden",
|
||||
"led",
|
||||
"30. leden 2012",
|
||||
"30. led 2012",
|
||||
};
|
||||
|
||||
public static void main(String[] args) {
|
||||
SimpleDateFormat fmt = new SimpleDateFormat("", CZECH);
|
||||
for (int i = 0; i < PATTERNS.length; i++) {
|
||||
fmt.applyPattern(PATTERNS[i]);
|
||||
String str = fmt.format(JAN30);
|
||||
if (!EXPECTED[i].equals(str)) {
|
||||
throw new RuntimeException("bad result: got '" + str
|
||||
+ "', expected '" + EXPECTED[i] + "'");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
1126
test/jdk/java/text/Format/DateFormat/DateFormatRegression.java
Normal file
1126
test/jdk/java/text/Format/DateFormat/DateFormatRegression.java
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -0,0 +1,738 @@
|
|||
/*
|
||||
* Copyright (c) 1997, 2024, 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
|
||||
* @summary test Date Format (Round Trip)
|
||||
* @bug 8008577 8174269
|
||||
* @run junit DateFormatRoundTripTest
|
||||
*/
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Random;
|
||||
import java.util.SimpleTimeZone;
|
||||
import java.util.TimeZone;
|
||||
|
||||
public class DateFormatRoundTripTest {
|
||||
|
||||
static Random RANDOM = null;
|
||||
|
||||
static final long FIXED_SEED = 3141592653589793238L; // Arbitrary fixed value
|
||||
|
||||
// Useful for turning up subtle bugs: Use -infinite and run while at lunch.
|
||||
boolean INFINITE = false; // Warning -- makes test run infinite loop!!!
|
||||
|
||||
boolean random = false;
|
||||
|
||||
// Options used to reproduce failures
|
||||
Locale locale = null;
|
||||
String pattern = null;
|
||||
Date initialDate = null;
|
||||
|
||||
Locale[] avail;
|
||||
TimeZone defaultZone;
|
||||
|
||||
// If SPARSENESS is > 0, we don't run each exhaustive possibility.
|
||||
// There are 24 total possible tests per each locale. A SPARSENESS
|
||||
// of 12 means we run half of them. A SPARSENESS of 23 means we run
|
||||
// 1 of them. SPARSENESS _must_ be in the range 0..23.
|
||||
static final int SPARSENESS = 18;
|
||||
|
||||
static final int TRIALS = 4;
|
||||
|
||||
static final int DEPTH = 5;
|
||||
|
||||
static SimpleDateFormat refFormat =
|
||||
new SimpleDateFormat("EEE MMM dd HH:mm:ss.SSS zzz yyyy G");
|
||||
|
||||
public DateFormatRoundTripTest(boolean rand, long seed, boolean infinite,
|
||||
Date date, String pat, Locale loc) {
|
||||
random = rand;
|
||||
if (random) {
|
||||
RANDOM = new Random(seed);
|
||||
}
|
||||
INFINITE = infinite;
|
||||
|
||||
initialDate = date;
|
||||
locale = loc;
|
||||
pattern = pat;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a name like "fr_FR" into Locale.of("fr", "FR", "");
|
||||
*/
|
||||
static Locale createLocale(String name) {
|
||||
String country = "",
|
||||
variant = "";
|
||||
int i;
|
||||
if ((i = name.indexOf('_')) >= 0) {
|
||||
country = name.substring(i+1);
|
||||
name = name.substring(0, i);
|
||||
}
|
||||
if ((i = country.indexOf('_')) >= 0) {
|
||||
variant = country.substring(i+1);
|
||||
country = country.substring(0, i);
|
||||
}
|
||||
return Locale.of(name, country, variant);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
// Command-line parameters
|
||||
Locale loc = null;
|
||||
boolean infinite = false;
|
||||
boolean random = false;
|
||||
long seed = FIXED_SEED;
|
||||
String pat = null;
|
||||
Date date = null;
|
||||
|
||||
List<String> newArgs = new ArrayList<>();
|
||||
for (int i=0; i<args.length; ++i) {
|
||||
if (args[i].equals("-locale")
|
||||
&& (i+1) < args.length) {
|
||||
loc = createLocale(args[i+1]);
|
||||
++i;
|
||||
} else if (args[i].equals("-date")
|
||||
&& (i+1) < args.length) {
|
||||
date = new Date(Long.parseLong(args[i+1]));
|
||||
++i;
|
||||
} else if (args[i].equals("-pattern")
|
||||
&& (i+1) < args.length) {
|
||||
pat = args[i+1];
|
||||
++i;
|
||||
} else if (args[i].equals("-INFINITE")) {
|
||||
infinite = true;
|
||||
} else if (args[i].equals("-random")) {
|
||||
random = true;
|
||||
} else if (args[i].equals("-randomseed")) {
|
||||
random = true;
|
||||
seed = System.currentTimeMillis();
|
||||
} else if (args[i].equals("-seed")
|
||||
&& (i+1) < args.length) {
|
||||
random = true;
|
||||
seed = Long.parseLong(args[i+1]);
|
||||
++i;
|
||||
} else {
|
||||
newArgs.add(args[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if (newArgs.size() != args.length) {
|
||||
args = new String[newArgs.size()];
|
||||
newArgs.addAll(Arrays.asList(args));
|
||||
}
|
||||
|
||||
new DateFormatRoundTripTest(random, seed, infinite, date, pat, loc)
|
||||
.TestDateFormatRoundTrip();
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a usage message for this test class.
|
||||
*/
|
||||
void usage() {
|
||||
System.out.println(getClass().getName() +
|
||||
": [-pattern <pattern>] [-locale <locale>] [-date <ms>] [-INFINITE]");
|
||||
System.out.println(" [-random | -randomseed | -seed <seed>]");
|
||||
System.out.println("* Warning: Some patterns will fail with some locales.");
|
||||
System.out.println("* Do not use -pattern unless you know what you are doing!");
|
||||
System.out.println("When specifying a locale, use a format such as fr_FR.");
|
||||
System.out.println("Use -pattern, -locale, and -date to reproduce a failure.");
|
||||
System.out.println("-random Random with fixed seed (same data every run).");
|
||||
System.out.println("-randomseed Random with a random seed.");
|
||||
System.out.println("-seed <s> Random using <s> as seed.");
|
||||
}
|
||||
|
||||
static private class TestCase {
|
||||
private int[] date;
|
||||
TimeZone zone;
|
||||
FormatFactory ff;
|
||||
boolean timeOnly;
|
||||
private Date _date;
|
||||
|
||||
TestCase(int[] d, TimeZone z, FormatFactory f, boolean timeOnly) {
|
||||
date = d;
|
||||
zone = z;
|
||||
ff = f;
|
||||
this.timeOnly = timeOnly;
|
||||
}
|
||||
|
||||
TestCase(Date d, TimeZone z, FormatFactory f, boolean timeOnly) {
|
||||
date = null;
|
||||
_date = d;
|
||||
zone = z;
|
||||
ff = f;
|
||||
this.timeOnly = timeOnly;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a format for testing.
|
||||
*/
|
||||
DateFormat createFormat() {
|
||||
return ff.createFormat();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the Date of this test case; must be called with the default
|
||||
* zone set to this TestCase's zone.
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
Date getDate() {
|
||||
if (_date == null) {
|
||||
// Date constructor will work right iff we are in the target zone
|
||||
int h = 0;
|
||||
int m = 0;
|
||||
int s = 0;
|
||||
if (date.length >= 4) {
|
||||
h = date[3];
|
||||
if (date.length >= 5) {
|
||||
m = date[4];
|
||||
if (date.length >= 6) {
|
||||
s = date[5];
|
||||
}
|
||||
}
|
||||
}
|
||||
_date = new Date(date[0] - 1900, date[1] - 1, date[2],
|
||||
h, m, s);
|
||||
}
|
||||
return _date;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return String.valueOf(getDate().getTime()) + " " +
|
||||
refFormat.format(getDate()) + " : " + ff.createFormat().format(getDate());
|
||||
}
|
||||
};
|
||||
|
||||
private interface FormatFactory {
|
||||
DateFormat createFormat();
|
||||
}
|
||||
|
||||
TestCase[] TESTS = {
|
||||
// Feb 29 2004 -- ordinary leap day
|
||||
new TestCase(new int[] {2004, 2, 29}, null,
|
||||
new FormatFactory() { public DateFormat createFormat() {
|
||||
return DateFormat.getDateTimeInstance(DateFormat.LONG,
|
||||
DateFormat.LONG);
|
||||
}}, false),
|
||||
|
||||
// Feb 29 2000 -- century leap day
|
||||
new TestCase(new int[] {2000, 2, 29}, null,
|
||||
new FormatFactory() { public DateFormat createFormat() {
|
||||
return DateFormat.getDateTimeInstance(DateFormat.LONG,
|
||||
DateFormat.LONG);
|
||||
}}, false),
|
||||
|
||||
// 0:00:00 Jan 1 1999 -- first second of normal year
|
||||
new TestCase(new int[] {1999, 1, 1}, null,
|
||||
new FormatFactory() { public DateFormat createFormat() {
|
||||
return DateFormat.getDateTimeInstance();
|
||||
}}, false),
|
||||
|
||||
// 23:59:59 Dec 31 1999 -- last second of normal year
|
||||
new TestCase(new int[] {1999, 12, 31, 23, 59, 59}, null,
|
||||
new FormatFactory() { public DateFormat createFormat() {
|
||||
return DateFormat.getDateTimeInstance();
|
||||
}}, false),
|
||||
|
||||
// 0:00:00 Jan 1 2004 -- first second of leap year
|
||||
new TestCase(new int[] {2004, 1, 1}, null,
|
||||
new FormatFactory() { public DateFormat createFormat() {
|
||||
return DateFormat.getDateTimeInstance();
|
||||
}}, false),
|
||||
|
||||
// 23:59:59 Dec 31 2004 -- last second of leap year
|
||||
new TestCase(new int[] {2004, 12, 31, 23, 59, 59}, null,
|
||||
new FormatFactory() { public DateFormat createFormat() {
|
||||
return DateFormat.getDateTimeInstance();
|
||||
}}, false),
|
||||
|
||||
// October 25, 1998 1:59:59 AM PDT -- just before DST cessation
|
||||
new TestCase(new Date(909305999000L), TimeZone.getTimeZone("PST"),
|
||||
new FormatFactory() { public DateFormat createFormat() {
|
||||
return DateFormat.getDateTimeInstance(DateFormat.LONG,
|
||||
DateFormat.LONG);
|
||||
}}, false),
|
||||
|
||||
// October 25, 1998 1:00:00 AM PST -- just after DST cessation
|
||||
new TestCase(new Date(909306000000L), TimeZone.getTimeZone("PST"),
|
||||
new FormatFactory() { public DateFormat createFormat() {
|
||||
return DateFormat.getDateTimeInstance(DateFormat.LONG,
|
||||
DateFormat.LONG);
|
||||
}}, false),
|
||||
|
||||
// April 4, 1999 1:59:59 AM PST -- just before DST onset
|
||||
new TestCase(new int[] {1999, 4, 4, 1, 59, 59},
|
||||
TimeZone.getTimeZone("PST"),
|
||||
new FormatFactory() { public DateFormat createFormat() {
|
||||
return DateFormat.getDateTimeInstance(DateFormat.LONG,
|
||||
DateFormat.LONG);
|
||||
}}, false),
|
||||
|
||||
// April 4, 1999 3:00:00 AM PDT -- just after DST onset
|
||||
new TestCase(new Date(923220000000L), TimeZone.getTimeZone("PST"),
|
||||
new FormatFactory() { public DateFormat createFormat() {
|
||||
return DateFormat.getDateTimeInstance(DateFormat.LONG,
|
||||
DateFormat.LONG);
|
||||
}}, false),
|
||||
|
||||
// October 4, 1582 11:59:59 PM PDT -- just before Gregorian change
|
||||
new TestCase(new int[] {1582, 10, 4, 23, 59, 59}, null,
|
||||
new FormatFactory() { public DateFormat createFormat() {
|
||||
return DateFormat.getDateTimeInstance(DateFormat.LONG,
|
||||
DateFormat.LONG);
|
||||
}}, false),
|
||||
|
||||
// October 15, 1582 12:00:00 AM PDT -- just after Gregorian change
|
||||
new TestCase(new int[] {1582, 10, 15, 0, 0, 0}, null,
|
||||
new FormatFactory() { public DateFormat createFormat() {
|
||||
return DateFormat.getDateTimeInstance(DateFormat.LONG,
|
||||
DateFormat.LONG);
|
||||
}}, false),
|
||||
};
|
||||
|
||||
public void TestDateFormatRoundTrip() {
|
||||
avail = DateFormat.getAvailableLocales();
|
||||
System.out.println("DateFormat available locales: " + avail.length);
|
||||
System.out.println("Default TimeZone: " +
|
||||
(defaultZone = TimeZone.getDefault()).getID());
|
||||
|
||||
if (random || initialDate != null) {
|
||||
if (RANDOM == null) {
|
||||
// Need this for sparse coverage to reduce combinatorial explosion,
|
||||
// even for non-random looped testing (i.e., with explicit date but
|
||||
// not pattern or locale).
|
||||
RANDOM = new Random(FIXED_SEED);
|
||||
}
|
||||
loopedTest();
|
||||
} else {
|
||||
for (int i=0; i<TESTS.length; ++i) {
|
||||
doTest(TESTS[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* TimeZone must be set to tc.zone before this method is called.
|
||||
*/
|
||||
private void doTestInZone(TestCase tc) {
|
||||
System.out.println(escape(tc.toString()));
|
||||
Locale save = Locale.getDefault();
|
||||
try {
|
||||
if (locale != null) {
|
||||
Locale.setDefault(locale);
|
||||
doTest(locale, tc.createFormat(), tc.timeOnly, tc.getDate());
|
||||
} else {
|
||||
for (int i=0; i<avail.length; ++i) {
|
||||
Locale.setDefault(avail[i]);
|
||||
doTest(avail[i], tc.createFormat(), tc.timeOnly, tc.getDate());
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
Locale.setDefault(save);
|
||||
}
|
||||
}
|
||||
|
||||
private void doTest(TestCase tc) {
|
||||
if (tc.zone == null) {
|
||||
// Just run in the default zone
|
||||
doTestInZone(tc);
|
||||
} else {
|
||||
try {
|
||||
TimeZone.setDefault(tc.zone);
|
||||
doTestInZone(tc);
|
||||
} finally {
|
||||
TimeZone.setDefault(defaultZone);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void loopedTest() {
|
||||
if (INFINITE) {
|
||||
// Special infinite loop test mode for finding hard to reproduce errors
|
||||
if (locale != null) {
|
||||
System.out.println("ENTERING INFINITE TEST LOOP, LOCALE " + locale.getDisplayName());
|
||||
for (;;) doTest(locale);
|
||||
} else {
|
||||
System.out.println("ENTERING INFINITE TEST LOOP, ALL LOCALES");
|
||||
for (;;) {
|
||||
for (int i=0; i<avail.length; ++i) {
|
||||
doTest(avail[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (locale != null) {
|
||||
doTest(locale);
|
||||
} else {
|
||||
doTest(Locale.getDefault());
|
||||
|
||||
for (int i=0; i<avail.length; ++i) {
|
||||
doTest(avail[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void doTest(Locale loc) {
|
||||
if (!INFINITE) System.out.println("Locale: " + loc.getDisplayName());
|
||||
|
||||
if (pattern != null) {
|
||||
doTest(loc, new SimpleDateFormat(pattern, loc));
|
||||
return;
|
||||
}
|
||||
|
||||
// Total possibilities = 24
|
||||
// 4 date
|
||||
// 4 time
|
||||
// 16 date-time
|
||||
boolean[] TEST_TABLE = new boolean[24];
|
||||
for (int i=0; i<24; ++i) TEST_TABLE[i] = true;
|
||||
|
||||
// If we have some sparseness, implement it here. Sparseness decreases
|
||||
// test time by eliminating some tests, up to 23.
|
||||
if (!INFINITE) {
|
||||
for (int i=0; i<SPARSENESS; ) {
|
||||
int random = (int)(java.lang.Math.random() * 24);
|
||||
if (random >= 0 && random < 24 && TEST_TABLE[i]) {
|
||||
TEST_TABLE[i] = false;
|
||||
++i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int itable = 0;
|
||||
for (int style=DateFormat.FULL; style<=DateFormat.SHORT; ++style) {
|
||||
if (TEST_TABLE[itable++])
|
||||
doTest(loc, DateFormat.getDateInstance(style, loc));
|
||||
}
|
||||
|
||||
for (int style=DateFormat.FULL; style<=DateFormat.SHORT; ++style) {
|
||||
if (TEST_TABLE[itable++])
|
||||
doTest(loc, DateFormat.getTimeInstance(style, loc), true);
|
||||
}
|
||||
|
||||
for (int dstyle=DateFormat.FULL; dstyle<=DateFormat.SHORT; ++dstyle) {
|
||||
for (int tstyle=DateFormat.FULL; tstyle<=DateFormat.SHORT; ++tstyle) {
|
||||
if (TEST_TABLE[itable++])
|
||||
doTest(loc, DateFormat.getDateTimeInstance(dstyle, tstyle, loc));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void doTest(Locale loc, DateFormat fmt) { doTest(loc, fmt, false); }
|
||||
|
||||
void doTest(Locale loc, DateFormat fmt, boolean timeOnly) {
|
||||
doTest(loc, fmt, timeOnly, initialDate != null ? initialDate : generateDate());
|
||||
}
|
||||
|
||||
void doTest(Locale loc, DateFormat fmt, boolean timeOnly, Date date) {
|
||||
// Skip testing with the JapaneseImperialCalendar which
|
||||
// doesn't support the Gregorian year semantices with 'y'.
|
||||
if (fmt.getCalendar().getClass().getName().equals("java.util.JapaneseImperialCalendar")) {
|
||||
return;
|
||||
}
|
||||
|
||||
String pat = ((SimpleDateFormat)fmt).toPattern();
|
||||
String deqPat = dequotePattern(pat); // Remove quoted elements
|
||||
|
||||
boolean hasEra = (deqPat.indexOf("G") != -1);
|
||||
boolean hasZone = (deqPat.indexOf("z") != -1);
|
||||
|
||||
Calendar cal = fmt.getCalendar();
|
||||
|
||||
// Because patterns contain incomplete data representing the Date,
|
||||
// we must be careful of how we do the roundtrip. We start with
|
||||
// a randomly generated Date because they're easier to generate.
|
||||
// From this we get a string. The string is our real starting point,
|
||||
// because this string should parse the same way all the time. Note
|
||||
// that it will not necessarily parse back to the original date because
|
||||
// of incompleteness in patterns. For example, a time-only pattern won't
|
||||
// parse back to the same date.
|
||||
|
||||
try {
|
||||
for (int i=0; i<TRIALS; ++i) {
|
||||
Date[] d = new Date[DEPTH];
|
||||
String[] s = new String[DEPTH];
|
||||
String error = null;
|
||||
|
||||
d[0] = date;
|
||||
|
||||
// We go through this loop until we achieve a match or until
|
||||
// the maximum loop count is reached. We record the points at
|
||||
// which the date and the string starts to match. Once matching
|
||||
// starts, it should continue.
|
||||
int loop;
|
||||
int dmatch = 0; // d[dmatch].getTime() == d[dmatch-1].getTime()
|
||||
int smatch = 0; // s[smatch].equals(s[smatch-1])
|
||||
for (loop=0; loop<DEPTH; ++loop) {
|
||||
if (loop > 0) d[loop] = fmt.parse(s[loop-1]);
|
||||
s[loop] = fmt.format(d[loop]);
|
||||
|
||||
if (loop > 0) {
|
||||
if (smatch == 0) {
|
||||
boolean match = s[loop].equals(s[loop-1]);
|
||||
if (smatch == 0) {
|
||||
if (match) smatch = loop;
|
||||
}
|
||||
else if (!match) {
|
||||
// This should never happen; if it does, fail.
|
||||
smatch = -1;
|
||||
error = "FAIL: String mismatch after match";
|
||||
}
|
||||
}
|
||||
|
||||
if (dmatch == 0) {
|
||||
boolean match = d[loop].getTime() == d[loop-1].getTime();
|
||||
if (dmatch == 0) {
|
||||
if (match) dmatch = loop;
|
||||
}
|
||||
else if (!match) {
|
||||
// This should never happen; if it does, fail.
|
||||
dmatch = -1;
|
||||
error = "FAIL: Date mismatch after match";
|
||||
}
|
||||
}
|
||||
|
||||
if (smatch != 0 && dmatch != 0) break;
|
||||
}
|
||||
}
|
||||
// At this point loop == DEPTH if we've failed, otherwise loop is the
|
||||
// max(smatch, dmatch), that is, the index at which we have string and
|
||||
// date matching.
|
||||
|
||||
// Date usually matches in 2. Exceptions handled below.
|
||||
int maxDmatch = 2;
|
||||
int maxSmatch = 1;
|
||||
if (dmatch > maxDmatch) {
|
||||
// Time-only pattern with zone information and a starting date in PST.
|
||||
if (timeOnly && hasZone && fmt.getTimeZone().inDaylightTime(d[0])) {
|
||||
maxDmatch = 3;
|
||||
maxSmatch = 2;
|
||||
}
|
||||
}
|
||||
|
||||
// String usually matches in 1. Exceptions are checked for here.
|
||||
if (smatch > maxSmatch) { // Don't compute unless necessary
|
||||
// Starts in BC, with no era in pattern
|
||||
if (!hasEra && getField(cal, d[0], Calendar.ERA) == GregorianCalendar.BC)
|
||||
maxSmatch = 2;
|
||||
// Starts in DST, no year in pattern
|
||||
else if (fmt.getTimeZone().inDaylightTime(d[0]) &&
|
||||
deqPat.indexOf("yyyy") == -1)
|
||||
maxSmatch = 2;
|
||||
// Two digit year with zone and year change and zone in pattern
|
||||
else if (hasZone &&
|
||||
fmt.getTimeZone().inDaylightTime(d[0]) !=
|
||||
fmt.getTimeZone().inDaylightTime(d[dmatch]) &&
|
||||
getField(cal, d[0], Calendar.YEAR) !=
|
||||
getField(cal, d[dmatch], Calendar.YEAR) &&
|
||||
deqPat.indexOf("y") != -1 &&
|
||||
deqPat.indexOf("yyyy") == -1)
|
||||
maxSmatch = 2;
|
||||
// Two digit year, year change, DST changeover hour. Example:
|
||||
// FAIL: Pattern: dd/MM/yy HH:mm:ss
|
||||
// Date matched in 2, wanted 2
|
||||
// String matched in 2, wanted 1
|
||||
// Thu Apr 02 02:35:52.110 PST 1795 AD F> 02/04/95 02:35:52
|
||||
// P> Sun Apr 02 01:35:52.000 PST 1995 AD F> 02/04/95 01:35:52
|
||||
// P> Sun Apr 02 01:35:52.000 PST 1995 AD F> 02/04/95 01:35:52 d== s==
|
||||
// The problem is that the initial time is not a DST onset day, but
|
||||
// then the year changes, and the resultant parsed time IS a DST
|
||||
// onset day. The hour "2:XX" makes no sense if 2:00 is the DST
|
||||
// onset, so DateFormat interprets it as 1:XX (arbitrary -- could
|
||||
// also be 3:XX, same problem). This results in an extra iteration
|
||||
// for String match convergence.
|
||||
else if (!justBeforeOnset(cal, d[0]) && justBeforeOnset(cal, d[dmatch]) &&
|
||||
getField(cal, d[0], Calendar.YEAR) !=
|
||||
getField(cal, d[dmatch], Calendar.YEAR) &&
|
||||
deqPat.indexOf("y") != -1 &&
|
||||
deqPat.indexOf("yyyy") == -1)
|
||||
maxSmatch = 2;
|
||||
// Another spurious failure:
|
||||
// FAIL: Pattern: dd MMMM yyyy hh:mm:ss
|
||||
// Date matched in 2, wanted 2
|
||||
// String matched in 2, wanted 1
|
||||
// Sun Apr 05 14:28:38.410 PDT 3998 AD F> 05 April 3998 02:28:38
|
||||
// P> Sun Apr 05 01:28:38.000 PST 3998 AD F> 05 April 3998 01:28:38
|
||||
// P> Sun Apr 05 01:28:38.000 PST 3998 AD F> 05 April 3998 01:28:38 d== s==
|
||||
// The problem here is that with an 'hh' pattern, hour from 1-12,
|
||||
// a lack of AM/PM -- that is, no 'a' in pattern, and an initial
|
||||
// time in the onset hour + 12:00.
|
||||
else if (deqPat.indexOf('h') >= 0
|
||||
&& deqPat.indexOf('a') < 0
|
||||
&& justBeforeOnset(cal, new Date(d[0].getTime() - 12*60*60*1000L))
|
||||
&& justBeforeOnset(cal, d[1]))
|
||||
maxSmatch = 2;
|
||||
}
|
||||
|
||||
if (dmatch > maxDmatch || smatch > maxSmatch
|
||||
|| dmatch < 0 || smatch < 0) {
|
||||
StringBuffer out = new StringBuffer();
|
||||
if (error != null) {
|
||||
out.append(error + '\n');
|
||||
}
|
||||
out.append("FAIL: Pattern: " + pat + ", Locale: " + loc + '\n');
|
||||
out.append(" Initial date (ms): " + d[0].getTime() + '\n');
|
||||
out.append(" Date matched in " + dmatch
|
||||
+ ", wanted " + maxDmatch + '\n');
|
||||
out.append(" String matched in " + smatch
|
||||
+ ", wanted " + maxSmatch);
|
||||
|
||||
for (int j=0; j<=loop && j<DEPTH; ++j) {
|
||||
out.append("\n " +
|
||||
(j>0?" P> ":" ") + refFormat.format(d[j]) + " F> " +
|
||||
escape(s[j]) +
|
||||
(j>0&&d[j].getTime()==d[j-1].getTime()?" d==":"") +
|
||||
(j>0&&s[j].equals(s[j-1])?" s==":""));
|
||||
}
|
||||
throw new RuntimeException(escape(out.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (ParseException e) {
|
||||
throw new RuntimeException(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a field of the given date
|
||||
*/
|
||||
static int getField(Calendar cal, Date d, int f) {
|
||||
// Should be synchronized, but we're single threaded so it's ok
|
||||
cal.setTime(d);
|
||||
return cal.get(f);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the given Date is in the 1 hour window BEFORE the
|
||||
* change from STD to DST for the given Calendar.
|
||||
*/
|
||||
static final boolean justBeforeOnset(Calendar cal, Date d) {
|
||||
return nearOnset(cal, d, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the given Date is in the 1 hour window AFTER the
|
||||
* change from STD to DST for the given Calendar.
|
||||
*/
|
||||
static final boolean justAfterOnset(Calendar cal, Date d) {
|
||||
return nearOnset(cal, d, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the given Date is in the 1 hour (or whatever the
|
||||
* DST savings is) window before or after the onset of DST.
|
||||
*/
|
||||
static boolean nearOnset(Calendar cal, Date d, boolean after) {
|
||||
cal.setTime(d);
|
||||
if ((cal.get(Calendar.DST_OFFSET) == 0) == after) {
|
||||
return false;
|
||||
}
|
||||
int delta;
|
||||
try {
|
||||
delta = ((SimpleTimeZone) cal.getTimeZone()).getDSTSavings();
|
||||
} catch (ClassCastException e) {
|
||||
delta = 60*60*1000; // One hour as ms
|
||||
}
|
||||
cal.setTime(new Date(d.getTime() + (after ? -delta : delta)));
|
||||
return (cal.get(Calendar.DST_OFFSET) == 0) == after;
|
||||
}
|
||||
|
||||
static String escape(String s) {
|
||||
StringBuffer buf = new StringBuffer();
|
||||
for (int i=0; i<s.length(); ++i) {
|
||||
char c = s.charAt(i);
|
||||
if (c < '\u0080') buf.append(c);
|
||||
else {
|
||||
buf.append("\\u");
|
||||
if (c < '\u1000') {
|
||||
buf.append('0');
|
||||
if (c < '\u0100') {
|
||||
buf.append('0');
|
||||
if (c < '\u0010') {
|
||||
buf.append('0');
|
||||
}
|
||||
}
|
||||
}
|
||||
buf.append(Integer.toHexString(c));
|
||||
}
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove quoted elements from a pattern. E.g., change "hh:mm 'o''clock'"
|
||||
* to "hh:mm ?". All quoted elements are replaced by one or more '?'
|
||||
* characters.
|
||||
*/
|
||||
static String dequotePattern(String pat) {
|
||||
StringBuffer out = new StringBuffer();
|
||||
boolean inQuote = false;
|
||||
for (int i=0; i<pat.length(); ++i) {
|
||||
char ch = pat.charAt(i);
|
||||
if (ch == '\'') {
|
||||
if ((i+1)<pat.length()
|
||||
&& pat.charAt(i+1) == '\'') {
|
||||
// Handle "''"
|
||||
out.append('?');
|
||||
++i;
|
||||
} else {
|
||||
inQuote = !inQuote;
|
||||
if (inQuote) {
|
||||
out.append('?');
|
||||
}
|
||||
}
|
||||
} else if (!inQuote) {
|
||||
out.append(ch);
|
||||
}
|
||||
}
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
static Date generateDate() {
|
||||
double a = (RANDOM.nextLong() & 0x7FFFFFFFFFFFFFFFL ) /
|
||||
((double)0x7FFFFFFFFFFFFFFFL);
|
||||
|
||||
// Now 'a' ranges from 0..1; scale it to range from 0 to 8000 years
|
||||
a *= 8000;
|
||||
|
||||
// Range from (4000-1970) BC to (8000-1970) AD
|
||||
a -= 4000;
|
||||
|
||||
// Now scale up to ms
|
||||
a *= 365.25 * 24 * 60 * 60 * 1000;
|
||||
|
||||
return new Date((long)a);
|
||||
}
|
||||
}
|
||||
|
||||
//eof
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
* Copyright (c) 2016, 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 8151431
|
||||
* @summary Make sure that clone() of a DateFormatSymbols subclass is not
|
||||
* called from DateFormatSymbols constructor.
|
||||
*/
|
||||
import java.text.DateFormatSymbols;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class DateFormatSymbolsCloneTest extends DateFormatSymbols {
|
||||
private int value;
|
||||
|
||||
public DateFormatSymbolsCloneTest() {
|
||||
value = 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object clone() {
|
||||
if (this.value == 0) {
|
||||
throw new RuntimeException("clone() should not be called from a DateFormatSymbols constructor");
|
||||
}
|
||||
return super.clone();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
new DateFormatSymbolsCloneTest();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,121 @@
|
|||
/*
|
||||
* Copyright (c) 1999, 2016, 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 4278402
|
||||
* @library /java/text/testlib
|
||||
* @build DateFormatSymbolsSerializationTest HexDumpReader
|
||||
* @run main DateFormatSymbolsSerializationTest
|
||||
* @summary Make sure DateFormatSymbols serialization
|
||||
*/
|
||||
|
||||
import java.util.*;
|
||||
import java.text.*;
|
||||
import java.io.*;
|
||||
|
||||
public class DateFormatSymbolsSerializationTest {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
Locale reservedLocale = Locale.getDefault();
|
||||
try {
|
||||
boolean err = false;
|
||||
Locale.setDefault(Locale.ENGLISH);
|
||||
SimpleDateFormat sdf =
|
||||
new SimpleDateFormat("yyyy.MM.dd E hh.mm.ss zzz",
|
||||
Locale.ENGLISH);
|
||||
Calendar calendar =
|
||||
new GregorianCalendar(TimeZone.getTimeZone("GMT"),
|
||||
Locale.ENGLISH);
|
||||
calendar.setTime(new Date(0L));
|
||||
DecimalFormat df = new DecimalFormat("");
|
||||
df.setDecimalSeparatorAlwaysShown(true);
|
||||
df.setGroupingSize(3);
|
||||
df.setMultiplier(1);
|
||||
df.setNegativePrefix("-");
|
||||
df.setNegativeSuffix("");
|
||||
df.setPositivePrefix("");
|
||||
df.setPositiveSuffix("");
|
||||
df.setMaximumFractionDigits(3); //
|
||||
df.setMinimumIntegerDigits(1); // for compatibility 1.2 and 1.3
|
||||
df.setMaximumIntegerDigits(40); //
|
||||
|
||||
sdf.setCalendar(calendar);
|
||||
sdf.setNumberFormat(df);
|
||||
|
||||
SimpleDateFormat sdf1;
|
||||
|
||||
if (args.length > 0) {
|
||||
try (FileOutputStream fos =
|
||||
new FileOutputStream("SDFserialized.ser")) {
|
||||
ObjectOutputStream oStream = new ObjectOutputStream(fos);
|
||||
oStream.writeObject(sdf);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
try (InputStream is =
|
||||
HexDumpReader.getStreamFromHexDump("SDFserialized.ser.txt")) {
|
||||
ObjectInputStream iStream = new ObjectInputStream(is);
|
||||
sdf1 = (SimpleDateFormat)iStream.readObject();
|
||||
}
|
||||
|
||||
DateFormatSymbols dfs = sdf.getDateFormatSymbols();
|
||||
DateFormatSymbols dfs1 = sdf1.getDateFormatSymbols();
|
||||
System.out.println(sdf + "," + sdf.toPattern());
|
||||
System.out.println(sdf1 + "," + sdf1.toPattern());
|
||||
|
||||
// time zone display names should not be a part of this
|
||||
// compatibility test. See 4112924 and 4282899.
|
||||
dfs.setZoneStrings(dfs1.getZoneStrings());
|
||||
// localPatternChars should not be a part of this
|
||||
// compatibility test. See 4322313.
|
||||
dfs.setLocalPatternChars(dfs1.getLocalPatternChars());
|
||||
sdf.setDateFormatSymbols(dfs);
|
||||
|
||||
// decimal format symbols should not be part of this
|
||||
// compatibility test - old decimal format symbols get filled
|
||||
// in with the root locale (4290801)
|
||||
DecimalFormat df1 = (DecimalFormat) sdf1.getNumberFormat();
|
||||
df1.setDecimalFormatSymbols(df.getDecimalFormatSymbols());
|
||||
|
||||
if (!dfs.equals(dfs1)) {
|
||||
err = true;
|
||||
System.err.println(
|
||||
"Error: serialized DateFormatSymbols is different");
|
||||
}
|
||||
if (!sdf.equals(sdf1)) {
|
||||
err = true;
|
||||
System.err.println(
|
||||
"Error: serialized SimpleDateFormat is different");
|
||||
}
|
||||
if (err) {
|
||||
throw new Exception("Serialization failed.");
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
// restore the reserved locale
|
||||
Locale.setDefault(reservedLocale);
|
||||
}
|
||||
}
|
||||
}
|
||||
1256
test/jdk/java/text/Format/DateFormat/DateFormatTest.java
Normal file
1256
test/jdk/java/text/Format/DateFormat/DateFormatTest.java
Normal file
File diff suppressed because it is too large
Load diff
232
test/jdk/java/text/Format/DateFormat/ISO8601ZoneTest.java
Normal file
232
test/jdk/java/text/Format/DateFormat/ISO8601ZoneTest.java
Normal file
|
|
@ -0,0 +1,232 @@
|
|||
/*
|
||||
* Copyright (c) 2010, 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 4919632
|
||||
* @summary Unit test for ISO8601 time zone format support
|
||||
*/
|
||||
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
|
||||
public class ISO8601ZoneTest {
|
||||
static final Date TIMESTAMP = new Date(1283758039020L);
|
||||
|
||||
static final String[][] formatData = {
|
||||
// time zone name, expected output at TIMESTAMP
|
||||
{ "America/Los_Angeles", "2010-09-06T00:27:19.020-07", },
|
||||
{ "America/Los_Angeles", "2010-09-06T00:27:19.020-0700", },
|
||||
{ "America/Los_Angeles", "2010-09-06T00:27:19.020-07:00", },
|
||||
{ "Australia/Sydney", "2010-09-06T17:27:19.020+10", },
|
||||
{ "Australia/Sydney", "2010-09-06T17:27:19.020+1000", },
|
||||
{ "Australia/Sydney", "2010-09-06T17:27:19.020+10:00", },
|
||||
{ "GMT-07:00", "2010-09-06T00:27:19.020-07", },
|
||||
{ "GMT-07:00", "2010-09-06T00:27:19.020-0700", },
|
||||
{ "GMT-07:00", "2010-09-06T00:27:19.020-07:00", },
|
||||
{ "UTC", "2010-09-06T07:27:19.020Z", },
|
||||
{ "UTC", "2010-09-06T07:27:19.020Z", },
|
||||
{ "UTC", "2010-09-06T07:27:19.020Z", },
|
||||
};
|
||||
|
||||
static final String[] zones = {
|
||||
"America/Los_Angeles", "Australia/Sydney", "GMT-07:00",
|
||||
"UTC", "GMT+05:30", "GMT-01:23",
|
||||
};
|
||||
|
||||
static final String[] isoZoneFormats = {
|
||||
"yyyy-MM-dd'T'HH:mm:ss.SSSX",
|
||||
"yyyy-MM-dd'T'HH:mm:ss.SSSXX",
|
||||
"yyyy-MM-dd'T'HH:mm:ss.SSSXXX",
|
||||
};
|
||||
|
||||
// badData[][0] - format
|
||||
// badData[][1] - (bad) text to be parsed
|
||||
// badData[][2] - subtext at the end of which a parse error is detected
|
||||
static final String[][] badData = {
|
||||
{ "X", "1", "1" },
|
||||
{ "X", "+1", "+1" },
|
||||
{ "X", "-2", "-2" },
|
||||
{ "X", "-24", "-2" },
|
||||
{ "X", "+24", "+2" },
|
||||
|
||||
{ "XX", "9", "9" },
|
||||
{ "XX", "23", "2" },
|
||||
{ "XX", "234", "2" },
|
||||
{ "XX", "3456", "3" },
|
||||
{ "XX", "23456", "2" },
|
||||
{ "XX", "+1", "+1" },
|
||||
{ "XX", "-12", "-12" },
|
||||
{ "XX", "+123", "+123" },
|
||||
{ "XX", "-12:34", "-12" },
|
||||
{ "XX", "+12:34", "+12" },
|
||||
{ "XX", "-2423", "-2" },
|
||||
{ "XX", "+2423", "+2" },
|
||||
{ "XX", "-1260", "-126" },
|
||||
{ "XX", "+1260", "+126" },
|
||||
|
||||
{ "XXX", "9", "9" },
|
||||
{ "XXX", "23", "2" },
|
||||
{ "XXX", "234", "2" },
|
||||
{ "XXX", "3456", "3" },
|
||||
{ "XXX", "23456", "2" },
|
||||
{ "XXX", "2:34", "2" },
|
||||
{ "XXX", "12:4", "1" },
|
||||
{ "XXX", "12:34", "1" },
|
||||
{ "XXX", "-1", "-1" },
|
||||
{ "XXX", "+1", "+1" },
|
||||
{ "XXX", "-12", "-12" },
|
||||
{ "XXX", "+12", "+12" },
|
||||
{ "XXX", "-123", "-12" },
|
||||
{ "XXX", "+123", "+12" },
|
||||
{ "XXX", "-1234", "-12" },
|
||||
{ "XXX", "+1234", "+12" },
|
||||
{ "XXX", "+24:23", "+2" },
|
||||
{ "XXX", "+12:60", "+12:6" },
|
||||
{ "XXX", "+1:23", "+1" },
|
||||
{ "XXX", "+12:3", "+12:3" },
|
||||
};
|
||||
|
||||
static String[] badFormats = {
|
||||
"XXXX", "XXXXX", "XXXXXX",
|
||||
};
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
TimeZone tz = TimeZone.getDefault();
|
||||
Locale loc = Locale.getDefault();
|
||||
Locale.setDefault(Locale.US);
|
||||
|
||||
try {
|
||||
for (int i = 0; i < formatData.length; i++) {
|
||||
TimeZone.setDefault(TimeZone.getTimeZone(formatData[i][0]));
|
||||
formatTest(isoZoneFormats[i % isoZoneFormats.length],
|
||||
formatData[i][1]);
|
||||
}
|
||||
|
||||
for (String zone : zones) {
|
||||
TimeZone.setDefault(TimeZone.getTimeZone(zone));
|
||||
for (String fmt : isoZoneFormats) {
|
||||
roundtripTest(fmt);
|
||||
SimpleDateFormat f = new SimpleDateFormat(fmt);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
for (String[] d : badData) {
|
||||
badDataParsing(d[0], d[1], d[2].length());
|
||||
}
|
||||
|
||||
for (String fmt : badFormats) {
|
||||
badFormat(fmt);
|
||||
}
|
||||
} finally {
|
||||
TimeZone.setDefault(tz);
|
||||
Locale.setDefault(loc);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static void formatTest(String fmt, String expected) throws Exception {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(fmt);
|
||||
String s = sdf.format(TIMESTAMP);
|
||||
if (!expected.equals(s)) {
|
||||
throw new RuntimeException("formatTest: got " + s
|
||||
+ ", expected " + expected);
|
||||
}
|
||||
|
||||
Date d = sdf.parse(s);
|
||||
if (d.getTime() != TIMESTAMP.getTime()) {
|
||||
throw new RuntimeException("formatTest: parse(" + s
|
||||
+ "), got " + d.getTime()
|
||||
+ ", expected " + TIMESTAMP.getTime());
|
||||
}
|
||||
|
||||
ParsePosition pos = new ParsePosition(0);
|
||||
d = sdf.parse(s + "123", pos);
|
||||
if (d.getTime() != TIMESTAMP.getTime()) {
|
||||
throw new RuntimeException("formatTest: parse(" + s
|
||||
+ "), got " + d.getTime()
|
||||
+ ", expected " + TIMESTAMP.getTime());
|
||||
}
|
||||
if (pos.getIndex() != s.length()) {
|
||||
throw new RuntimeException("formatTest: wrong resulting parse position: "
|
||||
+ pos.getIndex() + ", expected " + s.length());
|
||||
}
|
||||
}
|
||||
|
||||
static void roundtripTest(String fmt) throws Exception {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(fmt);
|
||||
Date date = new Date();
|
||||
|
||||
int fractionalHour = sdf.getTimeZone().getOffset(date.getTime());
|
||||
fractionalHour %= 3600000; // fraction of hour
|
||||
|
||||
String s = sdf.format(date);
|
||||
Date pd = sdf.parse(s);
|
||||
long diffsInMillis = pd.getTime() - date.getTime();
|
||||
if (diffsInMillis != 0) {
|
||||
if (diffsInMillis != fractionalHour) {
|
||||
throw new RuntimeException("fmt= " + fmt
|
||||
+ ", diff="+diffsInMillis
|
||||
+ ", fraction=" + fractionalHour);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void badDataParsing(String fmt, String text, int expectedErrorIndex) {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(fmt);
|
||||
try {
|
||||
sdf.parse(text);
|
||||
throw new RuntimeException("didn't throw an exception: fmt=" + fmt
|
||||
+ ", text=" + text);
|
||||
} catch (ParseException e) {
|
||||
// OK
|
||||
}
|
||||
|
||||
ParsePosition pos = new ParsePosition(0);
|
||||
Date d = sdf.parse(text, pos);
|
||||
int errorIndex = pos.getErrorIndex();
|
||||
if (d != null || errorIndex != expectedErrorIndex) {
|
||||
throw new RuntimeException("Bad error index=" + errorIndex
|
||||
+ ", expected=" + expectedErrorIndex
|
||||
+ ", fmt=" + fmt + ", text=" + text);
|
||||
}
|
||||
}
|
||||
|
||||
static void badFormat(String fmt) {
|
||||
try {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(fmt);
|
||||
throw new RuntimeException("Constructor didn't throw an exception: fmt=" + fmt);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// OK
|
||||
}
|
||||
try {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat();
|
||||
sdf.applyPattern(fmt);
|
||||
throw new RuntimeException("applyPattern didn't throw an exception: fmt=" + fmt);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// OK
|
||||
}
|
||||
}
|
||||
}
|
||||
276
test/jdk/java/text/Format/DateFormat/IntlTestDateFormat.java
Normal file
276
test/jdk/java/text/Format/DateFormat/IntlTestDateFormat.java
Normal file
|
|
@ -0,0 +1,276 @@
|
|||
/*
|
||||
* Copyright (c) 1998, 2024, 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
|
||||
* @summary test International Date Format
|
||||
* @bug 8008577 8174269
|
||||
* @run junit IntlTestDateFormat
|
||||
* @key randomness
|
||||
*/
|
||||
/*
|
||||
(C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
|
||||
(C) Copyright IBM Corp. 1996, 1997 - All Rights Reserved
|
||||
|
||||
The original version of this source code and documentation is copyrighted and
|
||||
owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These materials are
|
||||
provided under terms of a License Agreement between Taligent and Sun. This
|
||||
technology is protected by multiple US and International patents. This notice and
|
||||
attribution to Taligent may not be removed.
|
||||
Taligent is a registered trademark of Taligent, Inc.
|
||||
*/
|
||||
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
public class IntlTestDateFormat {
|
||||
// Values in milliseconds (== Date)
|
||||
private static final long ONESECOND = 1000;
|
||||
private static final long ONEMINUTE = 60 * ONESECOND;
|
||||
private static final long ONEHOUR = 60 * ONEMINUTE;
|
||||
private static final long ONEDAY = 24 * ONEHOUR;
|
||||
private static final double ONEYEAR = 365.25 * ONEDAY; // Approximate
|
||||
|
||||
// EModes
|
||||
private static final byte GENERIC = 0;
|
||||
private static final byte TIME = GENERIC + 1;
|
||||
private static final byte DATE = TIME + 1;
|
||||
private static final byte DATE_TIME = DATE + 1;
|
||||
|
||||
private DateFormat fFormat = DateFormat.getInstance();
|
||||
private String fTestName = new String("getInstance");
|
||||
private int fLimit = 3; // How many iterations it should take to reach convergence
|
||||
|
||||
@Test
|
||||
public void TestLocale() {
|
||||
localeTest(Locale.getDefault(), "Default Locale");
|
||||
}
|
||||
|
||||
// This test does round-trip testing (format -> parse -> format -> parse -> etc.) of DateFormat.
|
||||
public void localeTest(final Locale locale, final String localeName) {
|
||||
int timeStyle, dateStyle;
|
||||
|
||||
// For patterns including only time information and a timezone, it may take
|
||||
// up to three iterations, since the timezone may shift as the year number
|
||||
// is determined. For other patterns, 2 iterations should suffice.
|
||||
fLimit = 3;
|
||||
|
||||
for(timeStyle = 0; timeStyle < 4; timeStyle++) {
|
||||
fTestName = new String("Time test " + timeStyle + " (" + localeName + ")");
|
||||
try {
|
||||
fFormat = DateFormat.getTimeInstance(timeStyle, locale);
|
||||
}
|
||||
catch(StringIndexOutOfBoundsException e) {
|
||||
fail("FAIL: localeTest time getTimeInstance exception");
|
||||
throw e;
|
||||
}
|
||||
TestFormat();
|
||||
}
|
||||
|
||||
fLimit = 2;
|
||||
|
||||
for(dateStyle = 0; dateStyle < 4; dateStyle++) {
|
||||
fTestName = new String("Date test " + dateStyle + " (" + localeName + ")");
|
||||
try {
|
||||
fFormat = DateFormat.getDateInstance(dateStyle, locale);
|
||||
}
|
||||
catch(StringIndexOutOfBoundsException e) {
|
||||
fail("FAIL: localeTest date getTimeInstance exception");
|
||||
throw e;
|
||||
}
|
||||
TestFormat();
|
||||
}
|
||||
|
||||
for(dateStyle = 0; dateStyle < 4; dateStyle++) {
|
||||
for(timeStyle = 0; timeStyle < 4; timeStyle++) {
|
||||
fTestName = new String("DateTime test " + dateStyle + "/" + timeStyle + " (" + localeName + ")");
|
||||
try {
|
||||
fFormat = DateFormat.getDateTimeInstance(dateStyle, timeStyle, locale);
|
||||
}
|
||||
catch(StringIndexOutOfBoundsException e) {
|
||||
fail("FAIL: localeTest date/time getDateTimeInstance exception");
|
||||
throw e;
|
||||
}
|
||||
TestFormat();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void TestFormat() {
|
||||
if (fFormat == null) {
|
||||
fail("FAIL: DateFormat creation failed");
|
||||
return;
|
||||
}
|
||||
// logln("TestFormat: " + fTestName);
|
||||
Date now = new Date();
|
||||
tryDate(new Date(0));
|
||||
tryDate(new Date((long) 1278161801778.0));
|
||||
tryDate(now);
|
||||
// Shift 6 months into the future, AT THE SAME TIME OF DAY.
|
||||
// This will test the DST handling.
|
||||
tryDate(new Date(now.getTime() + 6*30*ONEDAY));
|
||||
|
||||
Date limit = new Date(now.getTime() * 10); // Arbitrary limit
|
||||
for (int i=0; i<2; ++i)
|
||||
// tryDate(new Date(floor(randDouble() * limit)));
|
||||
tryDate(new Date((long) (randDouble() * limit.getTime())));
|
||||
}
|
||||
|
||||
private void describeTest() {
|
||||
if (fFormat == null) {
|
||||
fail("FAIL: no DateFormat");
|
||||
return;
|
||||
}
|
||||
|
||||
// Assume it's a SimpleDateFormat and get some info
|
||||
SimpleDateFormat s = (SimpleDateFormat) fFormat;
|
||||
System.out.println(fTestName + " Pattern " + s.toPattern());
|
||||
}
|
||||
|
||||
private void tryDate(Date theDate) {
|
||||
final int DEPTH = 10;
|
||||
Date[] date = new Date[DEPTH];
|
||||
StringBuffer[] string = new StringBuffer[DEPTH];
|
||||
|
||||
int dateMatch = 0;
|
||||
int stringMatch = 0;
|
||||
boolean dump = false;
|
||||
int i;
|
||||
for (i=0; i<DEPTH; ++i) string[i] = new StringBuffer();
|
||||
for (i=0; i<DEPTH; ++i) {
|
||||
if (i == 0) date[i] = theDate;
|
||||
else {
|
||||
try {
|
||||
date[i] = fFormat.parse(string[i-1].toString());
|
||||
}
|
||||
catch (ParseException e) {
|
||||
describeTest();
|
||||
fail("********** FAIL: Parse of " + string[i-1] + " failed.");
|
||||
dump = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
FieldPosition position = new FieldPosition(0);
|
||||
fFormat.format(date[i], string[i], position);
|
||||
if (i > 0) {
|
||||
if (dateMatch == 0 && date[i] == date[i-1]) dateMatch = i;
|
||||
else if (dateMatch > 0 && date[i] != date[i-1]) {
|
||||
describeTest();
|
||||
fail("********** FAIL: Date mismatch after match.");
|
||||
dump = true;
|
||||
break;
|
||||
}
|
||||
if (stringMatch == 0 && string[i] == string[i-1]) stringMatch = i;
|
||||
else if (stringMatch > 0 && string[i] != string[i-1]) {
|
||||
describeTest();
|
||||
fail("********** FAIL: String mismatch after match.");
|
||||
dump = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (dateMatch > 0 && stringMatch > 0) break;
|
||||
}
|
||||
if (i == DEPTH) --i;
|
||||
|
||||
if (stringMatch > fLimit || dateMatch > fLimit) {
|
||||
describeTest();
|
||||
fail("********** FAIL: No string and/or date match within " + fLimit + " iterations.");
|
||||
dump = true;
|
||||
}
|
||||
|
||||
if (dump) {
|
||||
for (int k=0; k<=i; ++k) {
|
||||
System.out.println("" + k + ": " + date[k] + " F> " + string[k] + " P> ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Return a random double from 0.01 to 1, inclusive
|
||||
private double randDouble() {
|
||||
// Assume 8-bit (or larger) rand values. Also assume
|
||||
// that the system rand() function is very poor, which it always is.
|
||||
// double d;
|
||||
// int i;
|
||||
// do {
|
||||
// for (i=0; i < sizeof(double); ++i)
|
||||
// {
|
||||
// char poke = (char*)&d;
|
||||
// poke[i] = (rand() & 0xFF);
|
||||
// }
|
||||
// } while (TPlatformUtilities.isNaN(d) || TPlatformUtilities.isInfinite(d));
|
||||
|
||||
// if (d < 0.0) d = -d;
|
||||
// if (d > 0.0)
|
||||
// {
|
||||
// double e = floor(log10(d));
|
||||
// if (e < -2.0) d *= pow(10.0, -e-2);
|
||||
// else if (e > -1.0) d /= pow(10.0, e+1);
|
||||
// }
|
||||
// return d;
|
||||
Random rand = new Random();
|
||||
return rand.nextDouble();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void TestAvailableLocales() {
|
||||
final Locale[] locales = DateFormat.getAvailableLocales();
|
||||
long count = locales.length;
|
||||
System.out.println("" + count + " available locales");
|
||||
if (locales != null && count != 0) {
|
||||
StringBuffer all = new StringBuffer();
|
||||
for (int i=0; i<count; ++i) {
|
||||
if (i!=0) all.append(", ");
|
||||
all.append(locales[i].getDisplayName());
|
||||
}
|
||||
System.out.println(all.toString());
|
||||
}
|
||||
else fail("********** FAIL: Zero available locales or null array pointer");
|
||||
}
|
||||
|
||||
/* This test is too slow; we disable it for now
|
||||
@Test
|
||||
public void TestMonster() {
|
||||
final Locale[] locales = DateFormat.getAvailableLocales();
|
||||
long count = locales.length;
|
||||
if (locales != null && count != 0) {
|
||||
for (int i=0; i<count; ++i) {
|
||||
String name = locales[i].getDisplayName();
|
||||
System.out.println("Testing " + name + "...");
|
||||
try {
|
||||
localeTest(locales[i], name);
|
||||
}
|
||||
catch(Exception e) {
|
||||
fail("FAIL: TestMonster localeTest exception" + e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
//eof
|
||||
215
test/jdk/java/text/Format/DateFormat/IntlTestDateFormatAPI.java
Normal file
215
test/jdk/java/text/Format/DateFormat/IntlTestDateFormatAPI.java
Normal file
|
|
@ -0,0 +1,215 @@
|
|||
/*
|
||||
* Copyright (c) 1998, 2024, 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
|
||||
* @summary test International Date Format API
|
||||
* @bug 8008577 8174269
|
||||
* @run junit IntlTestDateFormatAPI
|
||||
*/
|
||||
/*
|
||||
(C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
|
||||
(C) Copyright IBM Corp. 1996, 1997 - All Rights Reserved
|
||||
|
||||
The original version of this source code and documentation is copyrighted and
|
||||
owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These materials are
|
||||
provided under terms of a License Agreement between Taligent and Sun. This
|
||||
technology is protected by multiple US and International patents. This notice and
|
||||
attribution to Taligent may not be removed.
|
||||
Taligent is a registered trademark of Taligent, Inc.
|
||||
*/
|
||||
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
public class IntlTestDateFormatAPI
|
||||
{
|
||||
|
||||
// Test that the equals method works correctly.
|
||||
@Test
|
||||
public void TestEquals()
|
||||
{
|
||||
// Create two objects at different system times
|
||||
DateFormat a = DateFormat.getInstance();
|
||||
Date start = Calendar.getInstance().getTime();
|
||||
while (start.equals(Calendar.getInstance().getTime())) ; // Wait for time to change
|
||||
DateFormat b = DateFormat.getInstance();
|
||||
|
||||
if (!(a.equals(b)))
|
||||
fail("FAIL: DateFormat objects created at different times are unequal.");
|
||||
|
||||
if (b instanceof SimpleDateFormat)
|
||||
{
|
||||
double ONE_YEAR = 365*24*60*60*1000.0;
|
||||
try {
|
||||
// ((SimpleDateFormat)b).setTwoDigitStartDate(start.getTime() + 50*ONE_YEAR);
|
||||
// if (a.equals(b))
|
||||
// errln("FAIL: DateFormat objects with different two digit start dates are equal.");
|
||||
}
|
||||
catch (Exception e) {
|
||||
fail("FAIL: setTwoDigitStartDate failed.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// This test checks various generic API methods in DateFormat to achieve 100% API coverage.
|
||||
@Test
|
||||
public void TestAPI()
|
||||
{
|
||||
System.out.println("DateFormat API test---"); System.out.println("");
|
||||
Locale.setDefault(Locale.ENGLISH);
|
||||
|
||||
|
||||
// ======= Test constructors
|
||||
|
||||
System.out.println("Testing DateFormat constructors");
|
||||
|
||||
DateFormat def = DateFormat.getInstance();
|
||||
DateFormat fr = DateFormat.getTimeInstance(DateFormat.FULL, Locale.FRENCH);
|
||||
DateFormat it = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.ITALIAN);
|
||||
DateFormat de = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.GERMAN);
|
||||
|
||||
// ======= Test equality
|
||||
|
||||
System.out.println("Testing equality operator");
|
||||
|
||||
if( fr.equals(it) ) {
|
||||
fail("ERROR: equals failed");
|
||||
}
|
||||
|
||||
// ======= Test various format() methods
|
||||
|
||||
System.out.println("Testing various format() methods");
|
||||
|
||||
Date d = new Date((long)837039928046.0);
|
||||
|
||||
StringBuffer res1 = new StringBuffer();
|
||||
StringBuffer res2 = new StringBuffer();
|
||||
String res3 = new String();
|
||||
FieldPosition pos1 = new FieldPosition(0);
|
||||
FieldPosition pos2 = new FieldPosition(0);
|
||||
|
||||
res1 = fr.format(d, res1, pos1);
|
||||
System.out.println("" + d.getTime() + " formatted to " + res1);
|
||||
|
||||
res2 = it.format(d, res2, pos2);
|
||||
System.out.println("" + d.getTime() + " formatted to " + res2);
|
||||
|
||||
res3 = de.format(d);
|
||||
System.out.println("" + d.getTime() + " formatted to " + res3);
|
||||
|
||||
// ======= Test parse()
|
||||
|
||||
System.out.println("Testing parse()");
|
||||
|
||||
String text = new String("02/03/76, 2:50 AM, CST");
|
||||
Object result1 = new Date();
|
||||
Date result2 = new Date();
|
||||
Date result3 = new Date();
|
||||
ParsePosition pos = new ParsePosition(0);
|
||||
ParsePosition pos01 = new ParsePosition(0);
|
||||
|
||||
result1 = def.parseObject(text, pos);
|
||||
if (result1 == null) {
|
||||
fail("ERROR: parseObject() failed for " + text);
|
||||
}
|
||||
System.out.println(text + " parsed into " + ((Date)result1).getTime());
|
||||
|
||||
try {
|
||||
result2 = def.parse(text);
|
||||
}
|
||||
catch (ParseException e) {
|
||||
fail("ERROR: parse() failed");
|
||||
}
|
||||
System.out.println(text + " parsed into " + result2.getTime());
|
||||
|
||||
result3 = def.parse(text, pos01);
|
||||
if (result3 == null) {
|
||||
fail("ERROR: parse() failed for " + text);
|
||||
}
|
||||
System.out.println(text + " parsed into " + result3.getTime());
|
||||
|
||||
|
||||
// ======= Test getters and setters
|
||||
|
||||
System.out.println("Testing getters and setters");
|
||||
|
||||
final Locale[] locales = DateFormat.getAvailableLocales();
|
||||
long count = locales.length;
|
||||
System.out.println("Got " + count + " locales" );
|
||||
for(int i = 0; i < count; i++) {
|
||||
String name;
|
||||
name = locales[i].getDisplayName();
|
||||
System.out.println(name);
|
||||
}
|
||||
|
||||
fr.setLenient(it.isLenient());
|
||||
if(fr.isLenient() != it.isLenient()) {
|
||||
fail("ERROR: setLenient() failed");
|
||||
}
|
||||
|
||||
final Calendar cal = def.getCalendar();
|
||||
Calendar newCal = (Calendar) cal.clone();
|
||||
de.setCalendar(newCal);
|
||||
it.setCalendar(newCal);
|
||||
if( ! de.getCalendar().equals(it.getCalendar())) {
|
||||
fail("ERROR: set Calendar() failed");
|
||||
}
|
||||
|
||||
final NumberFormat nf = def.getNumberFormat();
|
||||
NumberFormat newNf = (NumberFormat) nf.clone();
|
||||
de.setNumberFormat(newNf);
|
||||
it.setNumberFormat(newNf);
|
||||
if( ! de.getNumberFormat().equals(it.getNumberFormat())) {
|
||||
fail("ERROR: set NumberFormat() failed");
|
||||
}
|
||||
|
||||
final TimeZone tz = def.getTimeZone();
|
||||
TimeZone newTz = (TimeZone) tz.clone();
|
||||
de.setTimeZone(newTz);
|
||||
it.setTimeZone(newTz);
|
||||
if( ! de.getTimeZone().equals(it.getTimeZone())) {
|
||||
fail("ERROR: set TimeZone() failed");
|
||||
}
|
||||
|
||||
// ======= Test getStaticClassID()
|
||||
|
||||
// logln("Testing instanceof()");
|
||||
|
||||
// try {
|
||||
// DateFormat test = new SimpleDateFormat();
|
||||
|
||||
// if (! (test instanceof SimpleDateFormat)) {
|
||||
// errln("ERROR: instanceof failed");
|
||||
// }
|
||||
// }
|
||||
// catch (Exception e) {
|
||||
// errln("ERROR: Couldn't create a DateFormat");
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,216 @@
|
|||
/*
|
||||
* Copyright (c) 1998, 2025, 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
|
||||
* @summary test International Date Format Symbols
|
||||
* @bug 8366517
|
||||
* @run junit IntlTestDateFormatSymbols
|
||||
*/
|
||||
/*
|
||||
(C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
|
||||
(C) Copyright IBM Corp. 1996, 1997 - All Rights Reserved
|
||||
|
||||
The original version of this source code and documentation is copyrighted and
|
||||
owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These materials are
|
||||
provided under terms of a License Agreement between Taligent and Sun. This
|
||||
technology is protected by multiple US and International patents. This notice and
|
||||
attribution to Taligent may not be removed.
|
||||
Taligent is a registered trademark of Taligent, Inc.
|
||||
*/
|
||||
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
public class IntlTestDateFormatSymbols
|
||||
{
|
||||
// Test getMonths
|
||||
@Test
|
||||
public void TestGetMonths()
|
||||
{
|
||||
final String[] month;
|
||||
DateFormatSymbols symbol;
|
||||
|
||||
symbol=new DateFormatSymbols(Locale.getDefault());
|
||||
|
||||
month=symbol.getMonths();
|
||||
int cnt = month.length;
|
||||
|
||||
System.out.println("size = " + cnt);
|
||||
|
||||
for (int i=0; i<cnt; ++i)
|
||||
{
|
||||
System.out.println(month[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// Test the API of DateFormatSymbols; primarily a simple get/set set.
|
||||
@Test
|
||||
public void TestSymbols()
|
||||
{
|
||||
DateFormatSymbols fr = new DateFormatSymbols(Locale.FRENCH);
|
||||
|
||||
DateFormatSymbols en = new DateFormatSymbols(Locale.ENGLISH);
|
||||
|
||||
if(en.equals(fr)) {
|
||||
fail("ERROR: English DateFormatSymbols equal to French");
|
||||
}
|
||||
|
||||
// just do some VERY basic tests to make sure that get/set work
|
||||
|
||||
long count;
|
||||
final String[] eras = en.getEras();
|
||||
fr.setEras(eras);
|
||||
final String[] eras1 = fr.getEras();
|
||||
count = eras.length;
|
||||
if( count != eras1.length) {
|
||||
fail("ERROR: setEras() failed (different size array)");
|
||||
}
|
||||
else {
|
||||
for(int i = 0; i < count; i++) {
|
||||
if(! eras[i].equals(eras1[i])) {
|
||||
fail("ERROR: setEras() failed (different string values)");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
final String[] months = en.getMonths();
|
||||
fr.setMonths(months);
|
||||
final String[] months1 = fr.getMonths();
|
||||
count = months.length;
|
||||
if( count != months1.length) {
|
||||
fail("ERROR: setMonths() failed (different size array)");
|
||||
}
|
||||
else {
|
||||
for(int i = 0; i < count; i++) {
|
||||
if(! months[i].equals(months1[i])) {
|
||||
fail("ERROR: setMonths() failed (different string values)");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final String[] shortMonths = en.getShortMonths();
|
||||
fr.setShortMonths(shortMonths);
|
||||
final String[] shortMonths1 = fr.getShortMonths();
|
||||
count = shortMonths.length;
|
||||
if( count != shortMonths1.length) {
|
||||
fail("ERROR: setShortMonths() failed (different size array)");
|
||||
}
|
||||
else {
|
||||
for(int i = 0; i < count; i++) {
|
||||
if(! shortMonths[i].equals(shortMonths1[i])) {
|
||||
fail("ERROR: setShortMonths() failed (different string values)");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final String[] weekdays = en.getWeekdays();
|
||||
fr.setWeekdays(weekdays);
|
||||
final String[] weekdays1 = fr.getWeekdays();
|
||||
count = weekdays.length;
|
||||
if( count != weekdays1.length) {
|
||||
fail("ERROR: setWeekdays() failed (different size array)");
|
||||
}
|
||||
else {
|
||||
for(int i = 0; i < count; i++) {
|
||||
if(! weekdays[i].equals(weekdays1[i])) {
|
||||
fail("ERROR: setWeekdays() failed (different string values)");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final String[] shortWeekdays = en.getShortWeekdays();
|
||||
fr.setShortWeekdays(shortWeekdays);
|
||||
final String[] shortWeekdays1 = fr.getShortWeekdays();
|
||||
count = shortWeekdays.length;
|
||||
if( count != shortWeekdays1.length) {
|
||||
fail("ERROR: setShortWeekdays() failed (different size array)");
|
||||
}
|
||||
else {
|
||||
for(int i = 0; i < count; i++) {
|
||||
if(! shortWeekdays[i].equals(shortWeekdays1[i])) {
|
||||
fail("ERROR: setShortWeekdays() failed (different string values)");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final String[] ampms = en.getAmPmStrings();
|
||||
fr.setAmPmStrings(ampms);
|
||||
final String[] ampms1 = fr.getAmPmStrings();
|
||||
count = ampms.length;
|
||||
if( count != ampms1.length) {
|
||||
fail("ERROR: setAmPmStrings() failed (different size array)");
|
||||
}
|
||||
else {
|
||||
for(int i = 0; i < count; i++) {
|
||||
if(! ampms[i].equals(ampms1[i])) {
|
||||
fail("ERROR: setAmPmStrings() failed (different string values)");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
long rowCount = 0, columnCount = 0;
|
||||
final String[][] strings = en.getZoneStrings();
|
||||
fr.setZoneStrings(strings);
|
||||
final String[][] strings1 = fr.getZoneStrings();
|
||||
rowCount = strings.length;
|
||||
for(int i = 0; i < rowCount; i++) {
|
||||
columnCount = strings[i].length;
|
||||
for(int j = 0; j < columnCount; j++) {
|
||||
if( strings[i][j] != strings1[i][j] ) {
|
||||
fail("ERROR: setZoneStrings() failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// final String pattern = DateFormatSymbols.getPatternChars();
|
||||
|
||||
String localPattern, pat1, pat2;
|
||||
localPattern = en.getLocalPatternChars();
|
||||
fr.setLocalPatternChars(localPattern);
|
||||
if(! en.getLocalPatternChars().equals(fr.getLocalPatternChars())) {
|
||||
fail("ERROR: setLocalPatternChars() failed");
|
||||
}
|
||||
|
||||
|
||||
DateFormatSymbols foo = new DateFormatSymbols();
|
||||
|
||||
en = (DateFormatSymbols) fr.clone();
|
||||
|
||||
if(! en.equals(fr)) {
|
||||
fail("ERROR: Clone failed");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullLocaleTest() {
|
||||
assertThrows(NullPointerException.class, () -> new DateFormatSymbols(null));
|
||||
assertThrows(NullPointerException.class, () -> DateFormatSymbols.getInstance(null));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,184 @@
|
|||
/*
|
||||
* Copyright (c) 1998, 2024, 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
|
||||
* @summary test International Simple Date Format API
|
||||
* @bug 8008577 8174269
|
||||
* @run junit IntlTestSimpleDateFormatAPI
|
||||
*/
|
||||
/*
|
||||
(C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
|
||||
(C) Copyright IBM Corp. 1996, 1997 - All Rights Reserved
|
||||
|
||||
The original version of this source code and documentation is copyrighted and
|
||||
owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These materials are
|
||||
provided under terms of a License Agreement between Taligent and Sun. This
|
||||
technology is protected by multiple US and International patents. This notice and
|
||||
attribution to Taligent may not be removed.
|
||||
Taligent is a registered trademark of Taligent, Inc.
|
||||
*/
|
||||
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
public class IntlTestSimpleDateFormatAPI
|
||||
{
|
||||
|
||||
// This test checks various generic API methods in DecimalFormat to achieve 100% API coverage.
|
||||
@Test
|
||||
public void TestAPI()
|
||||
{
|
||||
System.out.println("SimpleDateFormat API test---"); System.out.println("");
|
||||
|
||||
Locale.setDefault(Locale.ENGLISH);
|
||||
|
||||
// ======= Test constructors
|
||||
|
||||
System.out.println("Testing SimpleDateFormat constructors");
|
||||
|
||||
SimpleDateFormat def = new SimpleDateFormat();
|
||||
|
||||
final String pattern = new String("yyyy.MM.dd G 'at' hh:mm:ss z");
|
||||
SimpleDateFormat pat = new SimpleDateFormat(pattern);
|
||||
|
||||
SimpleDateFormat pat_fr = new SimpleDateFormat(pattern, Locale.FRENCH);
|
||||
|
||||
DateFormatSymbols symbols = new DateFormatSymbols(Locale.FRENCH);
|
||||
|
||||
SimpleDateFormat cust1 = new SimpleDateFormat(pattern, symbols);
|
||||
|
||||
// ======= Test clone() and equality
|
||||
|
||||
System.out.println("Testing clone(), assignment and equality operators");
|
||||
|
||||
Format clone = (Format) def.clone();
|
||||
if( ! clone.equals(def) ) {
|
||||
fail("ERROR: Format clone or equals failed");
|
||||
}
|
||||
|
||||
// ======= Test various format() methods
|
||||
|
||||
System.out.println("Testing various format() methods");
|
||||
|
||||
Date d = new Date((long)837039928046.0);
|
||||
|
||||
StringBuffer res1 = new StringBuffer();
|
||||
StringBuffer res2 = new StringBuffer();
|
||||
FieldPosition pos1 = new FieldPosition(0);
|
||||
FieldPosition pos2 = new FieldPosition(0);
|
||||
|
||||
res1 = def.format(d, res1, pos1);
|
||||
System.out.println( "" + d.getTime() + " formatted to " + res1);
|
||||
|
||||
res2 = cust1.format(d, res2, pos2);
|
||||
System.out.println("" + d.getTime() + " formatted to " + res2);
|
||||
|
||||
// ======= Test parse()
|
||||
|
||||
System.out.println("Testing parse()");
|
||||
|
||||
String text = new String("02/03/76, 2:50 AM, CST");
|
||||
Date result1 = new Date();
|
||||
Date result2 = new Date();
|
||||
ParsePosition pos= new ParsePosition(0);
|
||||
result1 = def.parse(text, pos);
|
||||
System.out.println(text + " parsed into " + result1);
|
||||
|
||||
try {
|
||||
result2 = def.parse(text);
|
||||
}
|
||||
catch (ParseException e) {
|
||||
fail("ERROR: parse() failed");
|
||||
}
|
||||
System.out.println(text + " parsed into " + result2);
|
||||
|
||||
// ======= Test getters and setters
|
||||
|
||||
System.out.println("Testing getters and setters");
|
||||
|
||||
final DateFormatSymbols syms = pat.getDateFormatSymbols();
|
||||
def.setDateFormatSymbols(syms);
|
||||
pat_fr.setDateFormatSymbols(syms);
|
||||
if( ! pat.getDateFormatSymbols().equals(def.getDateFormatSymbols()) ) {
|
||||
fail("ERROR: set DateFormatSymbols() failed");
|
||||
}
|
||||
|
||||
Date startDate = null;
|
||||
try {
|
||||
// startDate = pat.getTwoDigitStartDate();
|
||||
}
|
||||
catch (Exception e) {
|
||||
fail("ERROR: getTwoDigitStartDate() failed");
|
||||
}
|
||||
|
||||
try {
|
||||
// pat_fr.setTwoDigitStartDate(startDate);
|
||||
}
|
||||
catch (Exception e) {
|
||||
fail("ERROR: setTwoDigitStartDate() failed");
|
||||
}
|
||||
|
||||
// ======= Test applyPattern()
|
||||
|
||||
System.out.println("Testing applyPattern()");
|
||||
|
||||
String p1 = new String("yyyy.MM.dd G 'at' hh:mm:ss z");
|
||||
System.out.println("Applying pattern " + p1);
|
||||
pat.applyPattern(p1);
|
||||
|
||||
String s2 = pat.toPattern();
|
||||
System.out.println("Extracted pattern is " + s2);
|
||||
if( ! s2.equals(p1) ) {
|
||||
fail("ERROR: toPattern() result did not match pattern applied");
|
||||
}
|
||||
|
||||
System.out.println("Applying pattern " + p1);
|
||||
pat.applyLocalizedPattern(p1);
|
||||
String s3 = pat.toLocalizedPattern();
|
||||
System.out.println("Extracted pattern is " + s3);
|
||||
if( ! s3.equals(p1) ) {
|
||||
fail("ERROR: toLocalizedPattern() result did not match pattern applied");
|
||||
}
|
||||
|
||||
// ======= Test getStaticClassID()
|
||||
|
||||
// logln("Testing instanceof");
|
||||
|
||||
// try {
|
||||
// DateFormat test = new SimpleDateFormat();
|
||||
|
||||
// if (! (test instanceof SimpleDateFormat)) {
|
||||
// errln("ERROR: instanceof failed");
|
||||
// }
|
||||
// }
|
||||
// catch (Exception e) {
|
||||
// errln("ERROR: Couldn't create a SimpleDateFormat");
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,141 @@
|
|||
/*
|
||||
* Copyright (c) 2024, 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 8324665
|
||||
* @summary Checks if SPACE_SEPARATOR are correctly parsed in lenient mode
|
||||
* @run junit LenientSpaceParsingTest
|
||||
*/
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.format.DateTimeFormatterBuilder;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class LenientSpaceParsingTest {
|
||||
@MethodSource
|
||||
private static Stream<Arguments> strictSpaces() {
|
||||
// input, pattern
|
||||
return Stream.of(
|
||||
Arguments.of("00\u002000", "H\u0020m"),
|
||||
Arguments.of("00\u202f00", "H\u202fm"),
|
||||
Arguments.of("00\u00a000", "H\u00a0m"),
|
||||
Arguments.of("00\u0020\u202f\u0020\u00a000", "H\u0020\u202f\u0020\u00a0m")
|
||||
);
|
||||
}
|
||||
|
||||
@MethodSource
|
||||
private static Stream<Arguments> lenientSpaces() {
|
||||
// input, pattern
|
||||
return Stream.of(
|
||||
Arguments.of("00\u002000", "H\u202fm"),
|
||||
Arguments.of("00\u202f00", "H\u0020m"),
|
||||
Arguments.of("00\u00a000", "H\u0020m"),
|
||||
Arguments.of("00\u002000", "H\u00a0m"),
|
||||
Arguments.of("00\u0020\u202f\u0020\u00a000", "H\u0020\u0020\u0020\u0020m"),
|
||||
Arguments.of("00\u0020\u202f\u0020\u00a000", "H\u202f\u00a0\u202f\u00a0m")
|
||||
);
|
||||
}
|
||||
|
||||
@MethodSource
|
||||
private static Stream<Arguments> nonSpaces() {
|
||||
// input, pattern
|
||||
return Stream.of(
|
||||
Arguments.of("00a00", "H\u202fm"),
|
||||
Arguments.of("00a00", "H\u00a0m"),
|
||||
Arguments.of("00a00", "H\u0020m"),
|
||||
Arguments.of("00aa00", "H\u0020\u0020m"),
|
||||
Arguments.of("00aa00", "H\u00a0\u202fm")
|
||||
);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource({"strictSpaces", "lenientSpaces"})
|
||||
public void checkDateTimeFormatter_Lenient(String input, String pattern) {
|
||||
new DateTimeFormatterBuilder().parseLenient().appendPattern(pattern).toFormatter().parse(input);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("nonSpaces")
|
||||
public void checkDateTimeFormatter_Lenient_Exception(String input, String pattern) {
|
||||
var dtf = new DateTimeFormatterBuilder().parseLenient().appendPattern(pattern).toFormatter();
|
||||
assertThrows(DateTimeParseException.class, () -> {
|
||||
dtf.parse(input);
|
||||
});
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("strictSpaces")
|
||||
public void checkDateTimeFormatter_Strict(String input, String pattern) {
|
||||
new DateTimeFormatterBuilder().parseStrict().appendPattern(pattern).toFormatter().parse(input);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource({"lenientSpaces", "nonSpaces"})
|
||||
public void checkDateTimeFormatter_Strict_Exception(String input, String pattern) {
|
||||
var dtf = new DateTimeFormatterBuilder().parseStrict().appendPattern(pattern).toFormatter();
|
||||
assertThrows(DateTimeParseException.class, () -> {
|
||||
dtf.parse(input);
|
||||
});
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource({"strictSpaces", "lenientSpaces"})
|
||||
public void checkSimpleDateFormat_Lenient(String input, String pattern) throws ParseException {
|
||||
new SimpleDateFormat(pattern).parse(input);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("nonSpaces")
|
||||
public void checkSimpleDateFormat_Lenient_Exception(String input, String pattern) {
|
||||
var sdf = new SimpleDateFormat(pattern);
|
||||
assertThrows(ParseException.class, () -> {
|
||||
sdf.parse(input);
|
||||
});
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("strictSpaces")
|
||||
public void checkSimpleDateFormat_Strict(String input, String pattern) throws ParseException {
|
||||
var sdf = new SimpleDateFormat(pattern);
|
||||
sdf.setLenient(false);
|
||||
sdf.parse(input);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource({"lenientSpaces", "nonSpaces"})
|
||||
public void checkSimpleDateFormat_Strict_Exception(String input, String pattern) {
|
||||
var sdf = new SimpleDateFormat(pattern);
|
||||
sdf.setLenient(false);
|
||||
assertThrows(ParseException.class, () -> {
|
||||
sdf.parse(input);
|
||||
});
|
||||
}
|
||||
}
|
||||
68
test/jdk/java/text/Format/DateFormat/LocaleDateFormats.java
Normal file
68
test/jdk/java/text/Format/DateFormat/LocaleDateFormats.java
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
/*
|
||||
* Copyright (c) 2015, 2025, 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 8080774 8174269
|
||||
* @modules jdk.localedata
|
||||
* @summary This file contains tests for JRE locales date formats
|
||||
* @run junit LocaleDateFormats
|
||||
*/
|
||||
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Locale;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
public class LocaleDateFormats {
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("dateFormats")
|
||||
void testDateFormat(Locale loc, int style, int year, int month, int date, String expectedString) {
|
||||
Calendar cal = Calendar.getInstance(loc);
|
||||
cal.set(year, month-1, date);
|
||||
// Create date formatter based on requested style and test locale
|
||||
DateFormat df = DateFormat.getDateInstance(style, loc);
|
||||
// Test the date format
|
||||
assertEquals(expectedString, df.format(cal.getTime()));
|
||||
}
|
||||
|
||||
private Object[][] dateFormats() {
|
||||
return new Object[][] {
|
||||
//8080774
|
||||
//Locale, Format type, year, month, date, expected result
|
||||
{localeEnSG, DateFormat.SHORT, 2015, 5, 6, "6/5/15"},
|
||||
{localeEnSG, DateFormat.MEDIUM, 2015, 5, 6, "6 May 2015"},
|
||||
{localeEnSG, DateFormat.LONG, 2015, 5, 6, "6 May 2015"},
|
||||
{localeEnSG, DateFormat.FULL, 2015, 5, 6, "Wednesday, 6 May 2015"}
|
||||
};
|
||||
}
|
||||
// en_SG Locale instance
|
||||
private static final Locale localeEnSG = Locale.of("en", "SG");
|
||||
}
|
||||
228
test/jdk/java/text/Format/DateFormat/NonGregorianFormatTest.java
Normal file
228
test/jdk/java/text/Format/DateFormat/NonGregorianFormatTest.java
Normal file
|
|
@ -0,0 +1,228 @@
|
|||
/*
|
||||
* Copyright (c) 2005, 2024, 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 4833268 6253991 8008577 8190748 8174269
|
||||
* @summary Test formatting and parsing with non-Gregorian calendars
|
||||
* @modules jdk.localedata
|
||||
* @run main NonGregorianFormatTest
|
||||
*/
|
||||
|
||||
import java.time.ZoneId;
|
||||
import java.util.*;
|
||||
import java.text.*;
|
||||
import static java.util.Calendar.*;
|
||||
|
||||
public class NonGregorianFormatTest {
|
||||
static int errors;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
static final Object[][] JAPANESE_EN = {
|
||||
{ "GGGG yyyy MMMM d", "Sh\u014dwa 1 December 31", new Date(1926-1900, DECEMBER, 31) },
|
||||
{ "GGGG yyyy MMMM d", "Sh\u014dwa 64 January 6", new Date(1989-1900, JANUARY, 6) },
|
||||
{ "GGGG yyyy MMMM d", "Heisei 1 August 9", new Date(1989-1900, AUGUST, 9) },
|
||||
{ "GGGG yyyy MMMM d", "Heisei 17 June 10", new Date(2005-1900, JUNE, 10) },
|
||||
{ "Gy.MM.dd", "Sh\u014dwa1.12.31", new Date(1926-1900, DECEMBER, 31) },
|
||||
{ "Gy.MM.dd", "Sh\u014dwa64.01.06", new Date(1989-1900, JANUARY, 6) },
|
||||
{ "Gyy.MM.dd", "Heisei01.08.09", new Date(1989-1900, AUGUST, 9) },
|
||||
{ "Gy.M.d", "Heisei1.8.9", new Date(1989-1900, AUGUST, 9) },
|
||||
{ "Gy.MM.dd", "Heisei17.06.10", new Date(2005-1900, JUNE, 10) },
|
||||
};
|
||||
|
||||
// Invalid dates for parse exception tests
|
||||
static final Object[][] EXCEPTION_JAPANESE_EN = {
|
||||
{ "GGGG yyyy MMMM d", "Sh\u014dwa 1 December 10" },
|
||||
{ "GGGG yyyy MMMM d", "Sh\u014dwa 64 January 16" },
|
||||
{ "GGGG yyyy MMMM d", "Heisei 1 January 1" },
|
||||
{ "Gy.MM.dd", "Sh\u014dwa1.12.10" },
|
||||
{ "Gy.MM.dd", "Sh\u014dwa64.01.16" },
|
||||
{ "Gyy.MM.dd", "Heisei01.01.01" },
|
||||
};
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
static final Object[][] BUDDHIST_EN = {
|
||||
{ "GGGG yyyy MMMM d", "BE 2469 December 31", new Date(1926-1900, DECEMBER, 31) },
|
||||
{ "GGGG yyyy MMMM d", "BE 2532 January 6", new Date(1989-1900, JANUARY, 6) },
|
||||
{ "GGGG yyyy MMMM d", "BE 2532 August 8", new Date(1989-1900, AUGUST, 8) },
|
||||
{ "GGGG yyyy MMMM d", "BE 2548 June 10", new Date(2005-1900, JUNE, 10) },
|
||||
{ "Gyyyy/MM/dd", "BE2469/12/31", new Date(1926-1900, DECEMBER, 31) },
|
||||
{ "Gyyyy/MM/dd", "BE2532/01/06", new Date(1989-1900, JANUARY, 6) },
|
||||
{ "Gyyyy/MM/dd", "BE2532/08/09", new Date(1989-1900, AUGUST, 9) },
|
||||
{ "Gyyyy/MM/dd", "BE2548/06/10", new Date(2005-1900, JUNE, 10) },
|
||||
};
|
||||
|
||||
static final String FULL_DATE_FORMAT_JA = "GGGGyyyy'\u5e74'M'\u6708'd'\u65e5'";
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
static final Object[][] JAPANESE_JA = {
|
||||
{ FULL_DATE_FORMAT_JA, "\u662d\u548c\u5143\u5e7412\u670831\u65e5", new Date(1926-1900, DECEMBER, 31) },
|
||||
{ FULL_DATE_FORMAT_JA, "\u662d\u548c64\u5e741\u67086\u65e5", new Date(1989-1900, JANUARY, 6) },
|
||||
{ FULL_DATE_FORMAT_JA, "\u5e73\u6210\u5143\u5e748\u67089\u65e5", new Date(1989-1900, AUGUST, 9) },
|
||||
{ FULL_DATE_FORMAT_JA, "\u5e73\u621017\u5e746\u670810\u65e5", new Date(2005-1900, JUNE, 10) },
|
||||
{ "Gyy.MM.dd", "\u662d\u548c01.12.31", new Date(1926-1900, DECEMBER, 31) },
|
||||
{ "Gyy.MM.dd", "\u662d\u548c64.01.06", new Date(1989-1900, JANUARY, 6) },
|
||||
{ "Gyy.MM.dd", "\u5e73\u621001.08.09", new Date(1989-1900, AUGUST, 9) },
|
||||
{ "Gy.M.d", "\u5e73\u62101.8.9", new Date(1989-1900, AUGUST, 9) },
|
||||
{ "Gyy.MM.dd", "\u5e73\u621017.06.10", new Date(2005-1900, JUNE, 10) },
|
||||
};
|
||||
|
||||
// Invalid dates for parse exception tests
|
||||
static final Object[][] EXCEPTION_JAPANESE_JA = {
|
||||
{ FULL_DATE_FORMAT_JA, "\u662d\u548c\u5143\u5e7412\u670810\u65e5" },
|
||||
{ FULL_DATE_FORMAT_JA, "\u662d\u548c64\u5e741\u670816\u65e5" },
|
||||
{ FULL_DATE_FORMAT_JA, "\u5e73\u6210\u5143\u5e741\u67081\u65e5" },
|
||||
{ "Gyy.MM.dd", "\u662d\u548c01.12.10" },
|
||||
{ "Gyy.MM.dd", "\u662d\u548c64.01.16" },
|
||||
{ "Gyy.MM.dd", "\u5e73\u621001.01.01" },
|
||||
};
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
static final Object[][] BUDDHIST_JA = {
|
||||
{ FULL_DATE_FORMAT_JA, "\u4ecf\u66a62469\u5e7412\u670831\u65e5", new Date(1926-1900, DECEMBER, 31) },
|
||||
{ FULL_DATE_FORMAT_JA, "\u4ecf\u66a62532\u5e741\u67086\u65e5", new Date(1989-1900, JANUARY, 6) },
|
||||
{ FULL_DATE_FORMAT_JA, "\u4ecf\u66a62532\u5e748\u67089\u65e5", new Date(1989-1900, AUGUST, 9) },
|
||||
{ FULL_DATE_FORMAT_JA, "\u4ecf\u66a62548\u5e746\u670810\u65e5", new Date(2005-1900, JUNE, 10) },
|
||||
{ "Gyyyy/MM/dd", "BE2469/12/31", new Date(1926-1900, DECEMBER, 31) },
|
||||
{ "Gyyyy/MM/dd", "BE2532/01/06", new Date(1989-1900, JANUARY, 6) },
|
||||
{ "Gyyyy/MM/dd", "BE2532/08/09", new Date(1989-1900, AUGUST, 9) },
|
||||
{ "Gyyyy/MM/dd", "BE2548/06/10", new Date(2005-1900, JUNE, 10) },
|
||||
};
|
||||
|
||||
public static void main(String[] args) throws ParseException {
|
||||
Locale defaultLocale = Locale.getDefault();
|
||||
Locale[] locales = { Locale.ENGLISH, Locale.JAPAN };
|
||||
try {
|
||||
for (Locale locale : locales) {
|
||||
test(locale);
|
||||
}
|
||||
} finally {
|
||||
Locale.setDefault(defaultLocale);
|
||||
}
|
||||
if (errors > 0) {
|
||||
throw new RuntimeException("FAILED: " + errors + " error(s)");
|
||||
}
|
||||
}
|
||||
|
||||
private static void test(Locale locale) {
|
||||
Locale.setDefault(locale);
|
||||
|
||||
// Tests with the Japanese imperial calendar
|
||||
Locale calendarLocale = Locale.of("ja", "JP", "JP");
|
||||
testRoundTrip(calendarLocale);
|
||||
testRoundTripSimple(calendarLocale,
|
||||
locale == Locale.ENGLISH ? JAPANESE_EN : JAPANESE_JA);
|
||||
testParseExceptions(calendarLocale,
|
||||
locale == Locale.ENGLISH ? EXCEPTION_JAPANESE_EN : EXCEPTION_JAPANESE_JA);
|
||||
|
||||
// Tests with the Thai Buddhist calendar
|
||||
calendarLocale = Locale.of("th", "TH");
|
||||
testRoundTrip(calendarLocale);
|
||||
testRoundTripSimple(calendarLocale,
|
||||
locale == Locale.ENGLISH ? BUDDHIST_EN : BUDDHIST_JA);
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private static void testRoundTrip(Locale calendarLocale) {
|
||||
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL,
|
||||
DateFormat.FULL,
|
||||
calendarLocale);
|
||||
|
||||
long t = System.currentTimeMillis();
|
||||
t = (t / 1000) * 1000; // discard milliseconds
|
||||
testRoundTrip(df, new Date(t));
|
||||
|
||||
// H1.8.9
|
||||
testRoundTrip(df, new Date(1989-1900, AUGUST, 9));
|
||||
|
||||
// H17.6.13
|
||||
testRoundTrip(df, new Date(2005-1900, JUNE, 13));
|
||||
}
|
||||
|
||||
private static void testRoundTrip(DateFormat df, Date orig) {
|
||||
try {
|
||||
var defZone = ZoneId.systemDefault();
|
||||
if (defZone.getRules().getTransition(orig.toInstant().atZone(defZone).toLocalDateTime()) != null) {
|
||||
System.out.println("At the offset transition. Round trip test skipped.");
|
||||
return;
|
||||
}
|
||||
String s = df.format(orig);
|
||||
Date parsed = df.parse(s);
|
||||
if (!orig.equals(parsed)) {
|
||||
error("testRoundTrip: bad date: original: '%s', parsed '%s'%n", orig, parsed);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
error("Unexpected exception: %s%n", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static void testRoundTripSimple(Locale calendarLocale, Object[][] data) {
|
||||
try {
|
||||
for (Object[] item : data) {
|
||||
String pattern = (String) item[0];
|
||||
String str = (String) item[1];
|
||||
Date date = (Date) item[2];
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
|
||||
Calendar cal = Calendar.getInstance(calendarLocale);
|
||||
sdf.setCalendar(cal);
|
||||
String s = sdf.format(date);
|
||||
if (!s.equals(str)) {
|
||||
error("testRoundTripSimple: Got '%s', expected '%s'%n", s, str);
|
||||
}
|
||||
Date d = sdf.parse(str);
|
||||
if (!d.equals(date)) {
|
||||
error("testRoundTripSimple: Got '%s', expected '%s'%n", d, date);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
error("Unexpected exception: %s%n", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static void testParseExceptions(Locale calendarLocale, Object[][] data) {
|
||||
for (Object[] item : data) {
|
||||
String pattern = (String) item[0];
|
||||
String str = (String) item[1];
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
|
||||
Calendar cal = Calendar.getInstance(calendarLocale);
|
||||
sdf.setCalendar(cal);
|
||||
sdf.setLenient(false);
|
||||
try {
|
||||
Date d = sdf.parse(str);
|
||||
error("testParseExceptions: parsing '%s' doesn't throw a ParseException.%n", str);
|
||||
} catch (ParseException e) {
|
||||
// OK
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void error(String msg) {
|
||||
System.out.println(msg);
|
||||
errors++;
|
||||
}
|
||||
|
||||
private static void error(String fmt, Object... args) {
|
||||
System.out.printf(fmt, args);
|
||||
errors++;
|
||||
}
|
||||
}
|
||||
372
test/jdk/java/text/Format/DateFormat/SDFTCKZoneNamesTest.java
Normal file
372
test/jdk/java/text/Format/DateFormat/SDFTCKZoneNamesTest.java
Normal file
|
|
@ -0,0 +1,372 @@
|
|||
/*
|
||||
* Copyright (c) 2019, 2025, 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 8218948 8347841
|
||||
* @summary TCK tests that check the time zone names between DFS.getZoneStrings()
|
||||
* and SDF.format("z*")
|
||||
* @run main SDFTCKZoneNamesTest
|
||||
*/
|
||||
import java.text.*;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
|
||||
public class SDFTCKZoneNamesTest {
|
||||
|
||||
StringBuffer myFormat(Date date, SimpleDateFormat sdf) {
|
||||
String pattern = sdf.toPattern();
|
||||
StringBuffer toAppendTo = new StringBuffer("");
|
||||
boolean inQuote = false;
|
||||
char prevCh = 0;
|
||||
char ch;
|
||||
int count = 0;
|
||||
for (int i = 0; i < pattern.length(); i++) {
|
||||
ch = pattern.charAt(i);
|
||||
if (inQuote) {
|
||||
if (ch == '\'') {
|
||||
inQuote = false;
|
||||
if (count == 0) toAppendTo.append(ch);
|
||||
else count = 0;
|
||||
} else {
|
||||
toAppendTo.append(ch);
|
||||
count++;
|
||||
}
|
||||
} else { // not inQuote
|
||||
if (ch == '\'') {
|
||||
inQuote = true;
|
||||
if (count > 0) {
|
||||
toAppendTo.append(subFormat(prevCh, count, date, sdf));
|
||||
count = 0;
|
||||
prevCh = 0;
|
||||
}
|
||||
} else if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z') {
|
||||
if (ch != prevCh && count > 0) {
|
||||
toAppendTo.append(subFormat(prevCh, count, date, sdf));
|
||||
prevCh = ch;
|
||||
count = 1;
|
||||
} else {
|
||||
if (ch != prevCh) prevCh = ch;
|
||||
count++;
|
||||
}
|
||||
} else if (count > 0) {
|
||||
toAppendTo.append(subFormat(prevCh, count, date, sdf));
|
||||
toAppendTo.append(ch);
|
||||
prevCh = 0;
|
||||
count = 0;
|
||||
} else toAppendTo.append(ch);
|
||||
}
|
||||
}
|
||||
if (count > 0) {
|
||||
toAppendTo.append(subFormat(prevCh, count, date, sdf));
|
||||
}
|
||||
return toAppendTo;
|
||||
}
|
||||
|
||||
private String subFormat(char ch, int count, Date date, SimpleDateFormat sdf)
|
||||
throws IllegalArgumentException {
|
||||
int value = 0;
|
||||
int patternCharIndex = -1;
|
||||
int maxIntCount = 10;
|
||||
String current = "";
|
||||
DateFormatSymbols formatData = sdf.getDateFormatSymbols();
|
||||
Calendar calendar = sdf.getCalendar();
|
||||
calendar.setTime(date);
|
||||
NumberFormat nf = sdf.getNumberFormat();
|
||||
nf.setGroupingUsed(false);
|
||||
|
||||
if ((patternCharIndex = "GyMdkHmsSEDFwWahKz".indexOf(ch)) == -1)
|
||||
throw new IllegalArgumentException("Illegal pattern character " +
|
||||
"'" + ch + "'");
|
||||
switch (patternCharIndex) {
|
||||
case 0: // 'G' - ERA
|
||||
value = calendar.get(Calendar.ERA);
|
||||
current = formatData.getEras()[value];
|
||||
break;
|
||||
case 1: // 'y' - YEAR
|
||||
value = calendar.get(Calendar.YEAR);
|
||||
|
||||
if (count == 2) {
|
||||
// For formatting, if the number of pattern letters is 2,
|
||||
// the year is truncated to 2 digits;
|
||||
current = zeroPaddingNumber(value, 2, 2, nf);
|
||||
} else {
|
||||
// otherwise it is interpreted as a number.
|
||||
current = zeroPaddingNumber(value, count, maxIntCount, nf);
|
||||
}
|
||||
|
||||
break;
|
||||
case 2: // 'M' - MONTH
|
||||
value = calendar.get(Calendar.MONTH);
|
||||
if (count >= 4)
|
||||
// DateFormatSymbols::getMonths spec: "If the language requires different forms for formatting
|
||||
// and stand-alone usages, this method returns month names in the formatting form."
|
||||
// Because of that only formatting cases patterns may be tested. Like, "MMMM yyyy". Wrong
|
||||
// pattern: "MMMM".
|
||||
current = formatData.getMonths()[value];
|
||||
else if (count == 3)
|
||||
// DateFormatSymbols::getShortMonths spec: "If the language requires different forms for formatting
|
||||
// and stand-alone usages, This method returns short month names in the formatting form."
|
||||
// Because of that only formatting cases patterns may be tested. Like, "MMM yyyy". Wrong pattern:
|
||||
// "MMM".
|
||||
current = formatData.getShortMonths()[value];
|
||||
else
|
||||
current = zeroPaddingNumber(value + 1, count, maxIntCount, nf);
|
||||
break;
|
||||
case 3: // 'd' - DATE
|
||||
value = calendar.get(Calendar.DATE);
|
||||
current = zeroPaddingNumber(value, count, maxIntCount, nf);
|
||||
break;
|
||||
case 4: // 'k' - HOUR_OF_DAY: 1-based. eg, 23:59 + 1 hour =>> 24:59
|
||||
if ((value = calendar.get(Calendar.HOUR_OF_DAY)) == 0)
|
||||
current = zeroPaddingNumber(
|
||||
calendar.getMaximum(Calendar.HOUR_OF_DAY) + 1,
|
||||
count, maxIntCount, nf);
|
||||
else
|
||||
current = zeroPaddingNumber(value, count, maxIntCount, nf);
|
||||
break;
|
||||
case 5: // 'H' - HOUR_OF_DAY:0-based. eg, 23:59 + 1 hour =>> 00:59
|
||||
value = calendar.get(Calendar.HOUR_OF_DAY);
|
||||
current = zeroPaddingNumber(value, count, maxIntCount, nf);
|
||||
break;
|
||||
case 6: // 'm' - MINUTE
|
||||
value = calendar.get(Calendar.MINUTE);
|
||||
current = zeroPaddingNumber(value, count, maxIntCount, nf);
|
||||
break;
|
||||
case 7: // 's' - SECOND
|
||||
value = calendar.get(Calendar.SECOND);
|
||||
current = zeroPaddingNumber(value, count, maxIntCount, nf);
|
||||
break;
|
||||
case 8: // 'S' - MILLISECOND
|
||||
value = calendar.get(Calendar.MILLISECOND);
|
||||
/*
|
||||
if (count > 3)
|
||||
value = value * (int) Math.pow(10, count - 3);
|
||||
else if (count == 2)
|
||||
value = (value + 5) / 10;
|
||||
else if (count == 1)
|
||||
value = (value + 50) / 100;
|
||||
*/
|
||||
current = zeroPaddingNumber(value, count, maxIntCount, nf);
|
||||
break;
|
||||
case 9: // 'E' - DAY_OF_WEEK
|
||||
value = calendar.get(Calendar.DAY_OF_WEEK);
|
||||
if (count >= 4)
|
||||
current = formatData.getWeekdays()[value];
|
||||
else // count < 4, use abbreviated form if exists
|
||||
current = formatData.getShortWeekdays()[value];
|
||||
break;
|
||||
case 10: // 'D' - DAY_OF_YEAR
|
||||
value = calendar.get(Calendar.DAY_OF_YEAR);
|
||||
current = zeroPaddingNumber(value, count, maxIntCount, nf);
|
||||
break;
|
||||
case 11: // 'F' - DAY_OF_WEEK_IN_MONTH
|
||||
value = calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH);
|
||||
current = zeroPaddingNumber(value, count, maxIntCount, nf);
|
||||
break;
|
||||
case 12: // 'w' - WEEK_OF_YEAR
|
||||
value = calendar.get(Calendar.WEEK_OF_YEAR);
|
||||
current = zeroPaddingNumber(value, count, maxIntCount, nf);
|
||||
break;
|
||||
case 13: // 'W' - WEEK_OF_MONTH
|
||||
value = calendar.get(Calendar.WEEK_OF_MONTH);
|
||||
current = zeroPaddingNumber(value, count, maxIntCount, nf);
|
||||
break;
|
||||
case 14: // 'a' - AM_PM
|
||||
value = calendar.get(Calendar.AM_PM);
|
||||
current = formatData.getAmPmStrings()[value];
|
||||
break;
|
||||
case 15: // 'h' - HOUR:1-based. eg, 11PM + 1 hour =>> 12 AM
|
||||
if ((value = calendar.get(Calendar.HOUR)) == 0)
|
||||
current = zeroPaddingNumber(
|
||||
calendar.getLeastMaximum(Calendar.HOUR) + 1,
|
||||
count, maxIntCount, nf);
|
||||
else
|
||||
current = zeroPaddingNumber(value, count, maxIntCount, nf);
|
||||
break;
|
||||
case 16: // 'K' - HOUR: 0-based. eg, 11PM + 1 hour =>> 0 AM
|
||||
value = calendar.get(Calendar.HOUR);
|
||||
current = zeroPaddingNumber(value, count, maxIntCount, nf);
|
||||
break;
|
||||
case 17: // 'z' - ZONE_OFFSET
|
||||
int zoneIndex = getZoneIndex(calendar.getTimeZone().getID(), formatData);
|
||||
if (zoneIndex == -1) {
|
||||
StringBuffer zoneString = new StringBuffer();
|
||||
value = calendar.get(Calendar.ZONE_OFFSET)
|
||||
+ calendar.get(Calendar.DST_OFFSET);
|
||||
if (value < 0) {
|
||||
zoneString.append("GMT-");
|
||||
value = -value; // suppress the '-' sign for text display.
|
||||
} else
|
||||
zoneString.append("GMT+");
|
||||
zoneString.append(
|
||||
zeroPaddingNumber((int) (value / (60 * 60 * 1000)), 2, 2, nf));
|
||||
zoneString.append(':');
|
||||
zoneString.append(
|
||||
zeroPaddingNumber(
|
||||
(int) ((value % (60 * 60 * 1000)) / (60 * 1000)), 2, 2, nf));
|
||||
current = zoneString.toString();
|
||||
} else if (calendar.get(Calendar.DST_OFFSET) != 0) {
|
||||
if (count >= 4)
|
||||
current = formatData.getZoneStrings()[zoneIndex][3];
|
||||
else
|
||||
// count < 4, use abbreviated form if exists
|
||||
current = formatData.getZoneStrings()[zoneIndex][4];
|
||||
} else {
|
||||
if (count >= 4)
|
||||
current = formatData.getZoneStrings()[zoneIndex][1];
|
||||
else
|
||||
current = formatData.getZoneStrings()[zoneIndex][2];
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return current;
|
||||
}
|
||||
|
||||
|
||||
String zeroPaddingNumber(long value, int minDigits, int maxDigits,
|
||||
NumberFormat nf) {
|
||||
nf.setMinimumIntegerDigits(minDigits);
|
||||
nf.setMaximumIntegerDigits(maxDigits);
|
||||
return nf.format(value);
|
||||
}
|
||||
|
||||
|
||||
int getZoneIndex(String ID, DateFormatSymbols dfs) {
|
||||
String[][] zoneStrings = dfs.getZoneStrings();
|
||||
|
||||
for (int index = 0; index < zoneStrings.length; index++) {
|
||||
if (ID.equalsIgnoreCase(zoneStrings[index][0])) return index;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
final int second = 1000;
|
||||
final int minute = 60 * second;
|
||||
final int hour = 60 * minute;
|
||||
final int day = 24 * hour;
|
||||
final int month = 30 * day;
|
||||
final int year = 365 * day;
|
||||
final int someday = 30 * year + 3 * month + 19 * day + 5 * hour;
|
||||
|
||||
|
||||
/* standalone interface */
|
||||
public static void main(String argv[]) {
|
||||
Locale defaultLocale = Locale.getDefault();
|
||||
SDFTCKZoneNamesTest test = new SDFTCKZoneNamesTest();
|
||||
|
||||
try {
|
||||
List.of(Locale.ROOT,
|
||||
Locale.CHINA,
|
||||
Locale.forLanguageTag("es-419"),
|
||||
Locale.GERMANY,
|
||||
Locale.forLanguageTag("hi-IN"),
|
||||
Locale.JAPAN,
|
||||
Locale.TAIWAN,
|
||||
Locale.UK,
|
||||
Locale.US,
|
||||
Locale.forLanguageTag("uz-Cyrl-UZ"),
|
||||
Locale.forLanguageTag("zh-SG"),
|
||||
Locale.forLanguageTag("zh-HK"),
|
||||
Locale.forLanguageTag("zh-MO")).stream()
|
||||
.forEach(l -> {
|
||||
System.out.printf("Testing locale: %s%n", l);
|
||||
Locale.setDefault(l);
|
||||
test.SimpleDateFormat0062();
|
||||
});
|
||||
} finally {
|
||||
Locale.setDefault(defaultLocale);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Equivalence class partitioning
|
||||
* with state, input and output values orientation
|
||||
* for public StringBuffer format(Date date, StringBuffer result, FieldPosition fp),
|
||||
* <br><b>pre-conditions</b>: patterns: { "'s0mething'z mm::hh,yyyy zz",
|
||||
* "zzzz",
|
||||
* "z"} (each pattern contains letter for TIMEZONE_FIELD),
|
||||
* <br><b>date</b>: a Date object
|
||||
* <br><b>result</b>: a string
|
||||
* <br><b>fp</b>: a FieldPosition object with TIMEZONE_FIELD field
|
||||
* <br><b>output</b>: formatted date as expected.
|
||||
*/
|
||||
public void SimpleDateFormat0062() {
|
||||
boolean passed = true;
|
||||
String patterns[] = {"'s0mething'z mm::hh,yyyy zz",
|
||||
"zzzz",
|
||||
"z"};
|
||||
SimpleDateFormat sdf = new SimpleDateFormat();
|
||||
Date date = new Date(1234567890);
|
||||
for (String[] tz : sdf.getDateFormatSymbols().getZoneStrings()) {
|
||||
if (ZoneId.SHORT_IDS.containsKey(tz[0])) {
|
||||
continue;
|
||||
}
|
||||
sdf.setTimeZone(TimeZone.getTimeZone(tz[0]));
|
||||
for (int i = 0; i < patterns.length && passed; i++) {
|
||||
StringBuffer result = new StringBuffer("qwerty");
|
||||
FieldPosition fp = new FieldPosition(DateFormat.TIMEZONE_FIELD);
|
||||
sdf.applyPattern(patterns[i]);
|
||||
String expected = new
|
||||
StringBuffer("qwerty").append(myFormat(date,
|
||||
sdf)).toString();
|
||||
String formatted = sdf.format(date, result, fp).toString();
|
||||
|
||||
if (!expected.equals(formatted)) {
|
||||
System.out.println(
|
||||
"method format(date, StringBuffer, FieldPosition) formats wrong");
|
||||
System.out.println(" pattern: " + patterns[i]);
|
||||
System.out.println(" time zone ID: " + tz[0]);
|
||||
System.out.println(" expected result: " + expected);
|
||||
System.out.println(" formatted result: " + formatted);
|
||||
passed = false;
|
||||
}
|
||||
|
||||
if (passed && !expected.equals(result.toString())) {
|
||||
System.out.println(
|
||||
"method format(Date date, StringBuffer toAppendTo, FieldPosition fp) toAppendTo is not " +
|
||||
"equal to output");
|
||||
System.out.println(" pattern: " + patterns[i]);
|
||||
System.out.println(" time zone ID: " + tz[0]);
|
||||
System.out.println(" toAppendTo : " + result);
|
||||
System.out.println(" formatted date: " + formatted);
|
||||
passed = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(passed)
|
||||
{
|
||||
System.out.println("PASSED : OKAY");
|
||||
}else
|
||||
{
|
||||
throw new RuntimeException("FAILED");
|
||||
}
|
||||
}
|
||||
}
|
||||
173
test/jdk/java/text/Format/DateFormat/SDFserialized.ser.txt
Normal file
173
test/jdk/java/text/Format/DateFormat/SDFserialized.ser.txt
Normal file
|
|
@ -0,0 +1,173 @@
|
|||
#
|
||||
# Copyright (c) 1999, 2016, 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.
|
||||
#
|
||||
|
||||
# Hex dump of a serialized DateFormatSymbols for
|
||||
# DateFormatSymbolsSerializationTest.
|
||||
|
||||
aced00057372001a6a6176612e746578742e53696d706c6544617465466f726d
|
||||
61744243c9da9394359002000449001573657269616c56657273696f6e4f6e53
|
||||
747265616d4c001364656661756c7443656e7475727953746172747400104c6a
|
||||
6176612f7574696c2f446174653b4c000a666f726d61744461746174001d4c6a
|
||||
6176612f746578742f44617465466f726d617453796d626f6c733b4c00077061
|
||||
747465726e7400124c6a6176612f6c616e672f537472696e673b787200146a61
|
||||
76612e746578742e44617465466f726d6174642ca1e4c22615fc0200024c0008
|
||||
63616c656e6461727400144c6a6176612f7574696c2f43616c656e6461723b4c
|
||||
000c6e756d626572466f726d61747400184c6a6176612f746578742f4e756d62
|
||||
6572466f726d61743b787200106a6176612e746578742e466f726d6174fbd8bc
|
||||
12e90f184302000078707372001b6a6176612e7574696c2e477265676f726961
|
||||
6e43616c656e6461728f3dd7d6e5b0d0c10200014a0010677265676f7269616e
|
||||
4375746f766572787200126a6176612e7574696c2e43616c656e646172e6ea4d
|
||||
1ec8dc5b8e03000b5a000c6172654669656c647353657449000e666972737444
|
||||
61794f665765656b5a0009697354696d655365745a00076c656e69656e744900
|
||||
166d696e696d616c44617973496e46697273745765656b4900096e6578745374
|
||||
616d7049001573657269616c56657273696f6e4f6e53747265616d4a00047469
|
||||
6d655b00066669656c64737400025b495b000569735365747400025b5a4c0004
|
||||
7a6f6e657400144c6a6176612f7574696c2f54696d655a6f6e653b7870010000
|
||||
000101010000000100000002000000010000000000000000757200025b494dba
|
||||
602676eab2a502000078700000001100000001000007b2000000000000000100
|
||||
0000010000000100000001000000050000000100000000000000000000000000
|
||||
00000000000000000000000000000000000000757200025b5a578f203914b85d
|
||||
e20200007870000000110101010101010101010101010101010101737200186a
|
||||
6176612e7574696c2e53696d706c6554696d655a6f6e65fa675d60d15ef5a603
|
||||
001049000a647374536176696e6773490006656e6444617949000c656e644461
|
||||
794f665765656b490007656e644d6f6465490008656e644d6f6e746849000765
|
||||
6e6454696d654900097261774f666673657449001573657269616c5665727369
|
||||
6f6e4f6e53747265616d490008737461727444617949000e7374617274446179
|
||||
4f665765656b49000973746172744d6f646549000a73746172744d6f6e746849
|
||||
0009737461727454696d654900097374617274596561725a000b757365446179
|
||||
6c696768745b000b6d6f6e74684c656e6774687400025b42787200126a617661
|
||||
2e7574696c2e54696d655a6f6e6531b3e9f57744aca10200014c000249447100
|
||||
7e00037870740003474d540036ee800000000000000000000000000000000000
|
||||
0000000000000000000001000000000000000000000000000000000000000000
|
||||
00000000757200025b42acf317f8060854e002000078700000000c1f1c1f1e1f
|
||||
1e1f1f1e1f1e1f770800000004000000007878fffff4e2f964ac00737200176a
|
||||
6176612e746578742e446563696d616c466f726d61740bff0362d872303a0200
|
||||
0b5a001b646563696d616c536570617261746f72416c7761797353686f776e42
|
||||
000c67726f7570696e6753697a654200116d696e4578706f6e656e7444696769
|
||||
747349000a6d756c7469706c69657249001573657269616c56657273696f6e4f
|
||||
6e53747265616d5a00167573654578706f6e656e7469616c4e6f746174696f6e
|
||||
4c000e6e6567617469766550726566697871007e00034c000e6e656761746976
|
||||
6553756666697871007e00034c000e706f73697469766550726566697871007e
|
||||
00034c000e706f73697469766553756666697871007e00034c000773796d626f
|
||||
6c737400204c6a6176612f746578742f446563696d616c466f726d617453796d
|
||||
626f6c733b787200166a6176612e746578742e4e756d626572466f726d6174df
|
||||
f6b3bf137d07e803000b5a000c67726f7570696e67557365644200116d617846
|
||||
72616374696f6e4469676974734200106d6178496e7465676572446967697473
|
||||
4900156d6178696d756d4672616374696f6e4469676974734900146d6178696d
|
||||
756d496e74656765724469676974734200116d696e4672616374696f6e446967
|
||||
6974734200106d696e496e74656765724469676974734900156d696e696d756d
|
||||
4672616374696f6e4469676974734900146d696e696d756d496e746567657244
|
||||
69676974735a00107061727365496e74656765724f6e6c794900157365726961
|
||||
6c56657273696f6e4f6e53747265616d7871007e000701032800000003000000
|
||||
2800010000000000000001000000000178010300000000010000000100740001
|
||||
2d74000071007e001f71007e001f7372001e6a6176612e746578742e44656369
|
||||
6d616c466f726d617453796d626f6c73501d17990868939c02000f4300106465
|
||||
63696d616c536570617261746f72430005646967697443000b6578706f6e656e
|
||||
7469616c43001167726f7570696e67536570617261746f724300096d696e7573
|
||||
5369676e4300116d6f6e6574617279536570617261746f724300107061747465
|
||||
726e536570617261746f724300077065724d696c6c43000770657263656e7449
|
||||
001573657269616c56657273696f6e4f6e53747265616d4300097a65726f4469
|
||||
6769744c00034e614e71007e00034c000e63757272656e637953796d626f6c71
|
||||
007e00034c0008696e66696e69747971007e00034c0012696e746c4375727265
|
||||
6e637953796d626f6c71007e00037870002e00230045002c002d002e003b2030
|
||||
0025000000010030740003efbfbd740002c2a4740003e2889e74000358585800
|
||||
0000017372000e6a6176612e7574696c2e44617465686a81014b597419030000
|
||||
78707708fffffe8f24e5cdd7787372001b6a6176612e746578742e4461746546
|
||||
6f726d617453796d626f6c73ace671c9351717c20200085b0005616d706d7374
|
||||
00135b4c6a6176612f6c616e672f537472696e673b5b00046572617371007e00
|
||||
294c00116c6f63616c5061747465726e436861727371007e00035b00066d6f6e
|
||||
74687371007e00295b000b73686f72744d6f6e74687371007e00295b000d7368
|
||||
6f72745765656b6461797371007e00295b00087765656b6461797371007e0029
|
||||
5b000b7a6f6e65537472696e67737400145b5b4c6a6176612f6c616e672f5374
|
||||
72696e673b7870757200135b4c6a6176612e6c616e672e537472696e673badd2
|
||||
56e7e91d7b47020000787000000002740002414d740002504d7571007e002c00
|
||||
0000027400024243740002414474001247794d646b486d735345444677576168
|
||||
4b7a7571007e002c0000000d7400074a616e7561727974000846656272756172
|
||||
797400054d61726368740005417072696c7400034d61797400044a756e657400
|
||||
044a756c7974000641756775737474000953657074656d6265727400074f6374
|
||||
6f6265727400084e6f76656d626572740008446563656d62657271007e001f75
|
||||
71007e002c0000000d7400034a616e7400034665627400034d61727400034170
|
||||
7271007e00397400034a756e7400034a756c7400034175677400035365707400
|
||||
034f63747400034e6f7674000344656371007e001f7571007e002c0000000871
|
||||
007e001f74000353756e7400034d6f6e74000354756574000357656474000354
|
||||
68757400034672697400035361747571007e002c0000000871007e001f740006
|
||||
53756e6461797400064d6f6e646179740007547565736461797400095765646e
|
||||
6573646179740008546875727364617974000646726964617974000853617475
|
||||
72646179757200145b5b4c6a6176612e6c616e672e537472696e673b324d09ad
|
||||
8432e45702000078700000001d7571007e002c00000005740003505354740015
|
||||
50616369666963205374616e646172642054696d6571007e0060740015506163
|
||||
69666963204461796c696768742054696d657400035044547571007e002c0000
|
||||
0005740013416d65726963612f4c6f735f416e67656c657371007e006171007e
|
||||
006071007e006271007e00637571007e002c000000057400034d53547400164d
|
||||
6f756e7461696e205374616e646172642054696d6571007e00677400164d6f75
|
||||
6e7461696e204461796c696768742054696d657400034d44547571007e002c00
|
||||
00000574000e416d65726963612f44656e76657271007e006871007e00677100
|
||||
7e006971007e006a7571007e002c00000005740003504e5471007e006871007e
|
||||
006771007e006871007e00677571007e002c0000000574000f416d6572696361
|
||||
2f50686f656e697871007e006871007e006771007e006871007e00677571007e
|
||||
002c0000000574000343535474001543656e7472616c205374616e6461726420
|
||||
54696d6571007e007274001543656e7472616c204461796c696768742054696d
|
||||
657400034344547571007e002c0000000574000f416d65726963612f43686963
|
||||
61676f71007e007371007e007271007e007471007e00757571007e002c000000
|
||||
057400034553547400154561737465726e205374616e646172642054696d6571
|
||||
007e00797400154561737465726e204461796c696768742054696d6574000345
|
||||
44547571007e002c00000005740010416d65726963612f4e65775f596f726b71
|
||||
007e007a71007e007971007e007b71007e007c7571007e002c00000005740003
|
||||
49455471007e007a71007e007971007e007a71007e00797571007e002c000000
|
||||
05740014416d65726963612f496e6469616e61706f6c697371007e007a71007e
|
||||
007971007e007a71007e00797571007e002c0000000574000348535474001448
|
||||
6177616969205374616e646172642054696d6571007e008471007e008571007e
|
||||
00847571007e002c00000005740010506163696669632f486f6e6f6c756c7571
|
||||
007e008571007e008471007e008571007e00847571007e002c00000005740003
|
||||
415354740014416c61736b61205374616e646172642054696d65740004414b53
|
||||
54740014416c61736b61204461796c696768742054696d65740004414b445475
|
||||
71007e002c00000005740011416d65726963612f416e63686f7261676571007e
|
||||
008a71007e008b71007e008c71007e008d7571007e002c0000000574000f416d
|
||||
65726963612f48616c6966617874001641746c616e746963205374616e646172
|
||||
642054696d6571007e008974001641746c616e746963204461796c6967687420
|
||||
54696d657400034144547571007e002c00000005740003434e5474001a4e6577
|
||||
666f756e646c616e64205374616e646172642054696d657400034e535474001a
|
||||
4e6577666f756e646c616e64204461796c696768742054696d657400034e4454
|
||||
7571007e002c00000005740010416d65726963612f53745f4a6f686e7371007e
|
||||
009771007e009871007e009971007e009a7571007e002c000000057400034543
|
||||
5474001e43656e7472616c204575726f7065616e205374616e64617264205469
|
||||
6d6574000343455474001e43656e7472616c204575726f7065616e204461796c
|
||||
696768742054696d65740004434553547571007e002c0000000574000c457572
|
||||
6f70652f506172697371007e009f71007e00a071007e00a171007e00a2757100
|
||||
7e002c0000000571007e0017740013477265656e77696368204d65616e205469
|
||||
6d6571007e001771007e00a671007e00177571007e002c000000057400114166
|
||||
726963612f43617361626c616e636171007e00a671007e001771007e00a67100
|
||||
7e00177571007e002c0000000574000e417369612f4a65727573616c656d7400
|
||||
1449737261656c205374616e646172642054696d657400034953547400144973
|
||||
7261656c204461796c696768742054696d657400034944547571007e002c0000
|
||||
00057400034a53547400134a6170616e205374616e646172642054696d657100
|
||||
7e00b071007e00b171007e00b07571007e002c0000000574000a417369612f54
|
||||
6f6b796f71007e00b171007e00b071007e00b171007e00b07571007e002c0000
|
||||
00057400104575726f70652f42756368617265737474001e4561737465726e20
|
||||
4575726f7065616e205374616e646172642054696d6574000345455474001e45
|
||||
61737465726e204575726f7065616e204461796c696768742054696d65740004
|
||||
454553547571007e002c000000057400034354547400134368696e6120537461
|
||||
6e646172642054696d6571007e007271007e00bc71007e00757571007e002c00
|
||||
00000574000d417369612f5368616e6768616971007e00bc71007e007271007e
|
||||
00bc71007e0075740019797979792e4d4d2e646420452068682e6d6d2e737320
|
||||
7a7a7a0a
|
||||
|
|
@ -0,0 +1,247 @@
|
|||
/*
|
||||
* Copyright (c) 2019, 2025, 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 4326988 6990146 8231213
|
||||
* @summary test SimpleDateFormat, check its pattern in the constructor
|
||||
* @run junit/othervm SimpleDateFormatPatternTest
|
||||
*/
|
||||
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.DateFormatSymbols;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Locale;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
public class SimpleDateFormatPatternTest {
|
||||
private static String[] validPat = {
|
||||
"yyyy-MM-dd h.mm.ss.a z",
|
||||
"yyyy'M'd' ahh'mm'ss' z",
|
||||
"dd MMMM yyyy hh:mm:ss",
|
||||
"d MMM yy HH:mm:ss",
|
||||
"dd/MM/yyyy HH:mm:ss",
|
||||
"d' / 'MMMM' / 'yyyy HH:mm:ss z",
|
||||
"d.M.yyyy H:mm:ss",
|
||||
"d' de 'MMMM' de 'yyyy H'h'm'min's's' z",
|
||||
"dd. MMMM yyyy HH:mm:ss z",
|
||||
"d-M-yyyy H:mm:ss",
|
||||
"EEEE''d MMMM G yyyy, H' 'm' 'ss' '",
|
||||
"dd.MMM.yyyy HH:mm:ss",
|
||||
"yy-MM-dd h:mm:ss.a",
|
||||
"d' de 'MMMM' de 'yyyy hh:mm:ss a z",
|
||||
"EEEE d MMMM yyyy H' h 'mm' min 'ss' s 'z",
|
||||
"d MMMM yyyy H:mm:ss",
|
||||
"d/MM/yyyy hh:mm:ss a",
|
||||
"EEEE, d, MMMM yyyy HH:mm:ss z",
|
||||
"EEEE, d. MMMM yyyy HH.mm.' h' z",
|
||||
"EEEE, d' / 'MMMM' / 'yyyy HH:mm:ss z",
|
||||
"d/MM/yyyy HH:mm:ss",
|
||||
"d MMMM yyyy H:mm:ss z",
|
||||
"MMMM d, yyyy h:mm:ss a z",
|
||||
"yyyy. MMMM d. H:mm:ss z",
|
||||
"d' de 'MMMM' de 'yyyy H:mm:ss z",
|
||||
"EEEE, MMMM d, yyyy h:mm:ss a z",
|
||||
"d/M/yyyy H:mm:ss",
|
||||
"d-MMM-yy HH:mm:ss",
|
||||
"EEEE d' de 'MMMM' de 'yyyy hh:mm:ss a z",
|
||||
"yyyy'M'd' ahh'mm'ss'",
|
||||
"yyyy'MM'dd' EEEE ahh'mm'ss'",
|
||||
"EEEE, d MMMM yyyy HH:mm:ss z",
|
||||
|
||||
//6990146: 'Y' for year; 'X' for time zone; 'u' for day number of the week
|
||||
"d/M/YYYY H:mm:ss",
|
||||
"d-MMM-YY HH:mm:ss",
|
||||
"EEEE d' de 'MMMM' de 'YYYY hh:mm:ss a X",
|
||||
"YYYY M d ahh:mm:ss",
|
||||
"YYYY MM dd EEEE u ahh/mm/ss",
|
||||
"EEEE, u, d MMMM YYYY HH:mm:ss X",
|
||||
"YYYY M d Z ahh mm ss",
|
||||
"YYYY-MM-dd EEEE u ahh-mm-ss",
|
||||
|
||||
//*added for sr-Latn*
|
||||
"EEEE, dd. MMMM y. HH.mm.ss zzzz",
|
||||
"dd. MMMM y. HH.mm.ss z",
|
||||
"dd.MM.y. HH.mm.ss",
|
||||
"d.M.yy. HH.mm"
|
||||
};
|
||||
|
||||
private static String[] invalidPat = {
|
||||
"yyyy'M'd' ahh:mm:ss",
|
||||
"EEEe d MM MM yyyy HH' h 'mm zzzZ",
|
||||
"d MMMM\\ yyyy, H' 'm' 'g",
|
||||
"EEEE d' @# MMMMde 'yyys HHH'mm z",
|
||||
"yyyy'MMe 2 #dd' EEEEahh'mm'ss' z,z",
|
||||
"yyyy.M.d H;mm.ses",
|
||||
"EEEe, d MMMM yyyy h:mm:ss a z",
|
||||
"EEEE, MMMM d, 'y y y y h:mm:ss 'o''clock' a z",
|
||||
"dd MMMM yyyy 0HHcl:mm:ss z",
|
||||
"d.M_M_y.yy1yy HextH:mm|45:",
|
||||
"d,D MMMTTTTTTTTTKM yy|+yy HH:m m:ss z",
|
||||
"d-MDtM M-yy H:mm:ss",
|
||||
"yyyy/M///m/nM/d Dd H:m$m:s s",
|
||||
"EEEE, dd. MMMM yyyy HH:m'''m' Uhr 'z",
|
||||
//6990146
|
||||
"EEEE d' de 'MMMM' de 'YYYY hh:mm:ss a x",
|
||||
"EEEE, U, d MMMM YYYY HH:mm:ss Z"
|
||||
};
|
||||
|
||||
private static Locale[] locales = DateFormat.getAvailableLocales();
|
||||
private static Object[][] dfAllLocalesObj = createAllLocales();
|
||||
private static Object[][] invalidPatObj = createPatternObj(invalidPat);
|
||||
private static Object[][] validPatObj = createPatternObj(validPat);
|
||||
|
||||
private static Object[][] createAllLocales() {
|
||||
Object[][] objArray = new Object[locales.length][];
|
||||
for (int i = 0; i < locales.length; i++) {
|
||||
objArray[i] = new Object[1];
|
||||
objArray[i][0] = locales[i];
|
||||
}
|
||||
return objArray;
|
||||
}
|
||||
|
||||
private static Object[][] createPatternObj(String[] pattern){
|
||||
Object[][] objArray = new Object[locales.length * pattern.length][];
|
||||
int k = 0;
|
||||
for (int i = 0; i < locales.length; i++) {
|
||||
for (int j = 0; j < pattern.length; j++) {
|
||||
objArray[k] = new Object[2];
|
||||
objArray[k][0] = pattern[j];
|
||||
objArray[k][1] = locales[i];
|
||||
k = k + 1;
|
||||
}
|
||||
}
|
||||
return objArray;
|
||||
}
|
||||
|
||||
Object[][] dfAllLocalesObj() {
|
||||
return dfAllLocalesObj;
|
||||
}
|
||||
|
||||
Object[][] invalidPatternObj() {
|
||||
return invalidPatObj;
|
||||
}
|
||||
|
||||
Object[][] validPatternObj() {
|
||||
return validPatObj;
|
||||
}
|
||||
|
||||
//check Constructors for invalid pattern
|
||||
@ParameterizedTest
|
||||
@MethodSource("invalidPatternObj")
|
||||
void testIllegalArgumentException1(String pattern, Locale loc)
|
||||
throws IllegalArgumentException {
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
Locale.setDefault(loc);
|
||||
new SimpleDateFormat(pattern);
|
||||
});
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("invalidPatternObj")
|
||||
void testIllegalArgumentException2(String pattern, Locale loc)
|
||||
throws IllegalArgumentException {
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
Locale.setDefault(loc);
|
||||
new SimpleDateFormat(pattern, new DateFormatSymbols());
|
||||
});
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("invalidPatternObj")
|
||||
void testIllegalArgumentException3 (String pattern, Locale loc)
|
||||
throws IllegalArgumentException {
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
Locale.setDefault(loc);
|
||||
new SimpleDateFormat(pattern, Locale.getDefault());
|
||||
});
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("invalidPatternObj")
|
||||
void testIllegalArgumentException4(String pattern, Locale loc)
|
||||
throws IllegalArgumentException {
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
Locale.setDefault(loc);
|
||||
new SimpleDateFormat().applyPattern(pattern);
|
||||
});
|
||||
}
|
||||
|
||||
//check Constructors for null pattern
|
||||
@ParameterizedTest
|
||||
@MethodSource("dfAllLocalesObj")
|
||||
void testNullPointerException1(Locale loc)
|
||||
throws NullPointerException {
|
||||
assertThrows(NullPointerException.class, () -> {
|
||||
Locale.setDefault(loc);
|
||||
new SimpleDateFormat(null);
|
||||
});
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("dfAllLocalesObj")
|
||||
void testNullPointerException2(Locale loc)
|
||||
throws NullPointerException {
|
||||
assertThrows(NullPointerException.class, () -> {
|
||||
Locale.setDefault(loc);
|
||||
new SimpleDateFormat(null, new DateFormatSymbols());
|
||||
});
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("dfAllLocalesObj")
|
||||
void testNullPointerException3(Locale loc)
|
||||
throws NullPointerException {
|
||||
assertThrows(NullPointerException.class, () -> {
|
||||
Locale.setDefault(loc);
|
||||
new SimpleDateFormat(null, Locale.getDefault());
|
||||
});
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("dfAllLocalesObj")
|
||||
void testNullPointerException4(Locale loc)
|
||||
throws NullPointerException {
|
||||
assertThrows(NullPointerException.class, () -> {
|
||||
Locale.setDefault(loc);
|
||||
new SimpleDateFormat().applyPattern(null);
|
||||
});
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
//check Constructors for valid pattern
|
||||
@MethodSource("validPatternObj")
|
||||
void testValidPattern(String pattern, Locale loc) {
|
||||
Locale.setDefault(loc);
|
||||
new SimpleDateFormat(pattern);
|
||||
new SimpleDateFormat(pattern, new DateFormatSymbols());
|
||||
new SimpleDateFormat(pattern, Locale.getDefault());
|
||||
new SimpleDateFormat().applyPattern(pattern);
|
||||
}
|
||||
}
|
||||
153
test/jdk/java/text/Format/DateFormat/TimeZoneNameTest.java
Normal file
153
test/jdk/java/text/Format/DateFormat/TimeZoneNameTest.java
Normal file
|
|
@ -0,0 +1,153 @@
|
|||
/*
|
||||
* Copyright (c) 2000, 2024, 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 4348864 4112924 4425386 4495052 4836940 4851113 8008577 8174269
|
||||
* @summary test time zone display names in en_US locale
|
||||
* @run junit TimeZoneNameTest
|
||||
*/
|
||||
|
||||
import java.util.*;
|
||||
import java.text.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
public class TimeZoneNameTest
|
||||
{
|
||||
|
||||
// Change JVM default Locale
|
||||
@BeforeAll
|
||||
static void initAll() {
|
||||
Locale.setDefault(Locale.US);
|
||||
}
|
||||
|
||||
static final String[] data = {
|
||||
// Added to verify the fix for 4836940
|
||||
"N", "Antarctica/Rothera", "GMT-03:00", "Rothera Time", "GMT-03:00", "Rothera Time",
|
||||
"N", "Asia/Tehran", "GMT+03:30", "Iran Standard Time", "GMT+04:30", "Iran Daylight Time",
|
||||
"N", "Iran", "GMT+03:30", "Iran Standard Time", "GMT+04:30", "Iran Daylight Time",
|
||||
|
||||
// Added to verify the fix for 4851113
|
||||
"N", "America/Rankin_Inlet", "CST", "Central Standard Time", "CDT", "Central Daylight Time",
|
||||
"N", "Asia/Samarkand", "GMT+05:00", "Uzbekistan Standard Time", "GMT+05:00", "Uzbekistan Standard Time",
|
||||
"N", "Asia/Tashkent", "GMT+05:00", "Uzbekistan Standard Time", "GMT+05:00", "Uzbekistan Standard Time",
|
||||
"N", "Atlantic/Jan_Mayen", "CET", "Central European Standard Time", "CEST", "Central European Summer Time",
|
||||
"N", "Europe/Oslo", "CET", "Central European Standard Time", "CEST", "Central European Summer Time",
|
||||
|
||||
"N", "Pacific/Honolulu", "HST", "Hawaii-Aleutian Standard Time", "HST", "Hawaii-Aleutian Standard Time",
|
||||
"N", "America/Los_Angeles", "PST", "Pacific Standard Time", "PDT", "Pacific Daylight Time",
|
||||
"N", "US/Pacific", "PST", "Pacific Standard Time", "PDT", "Pacific Daylight Time",
|
||||
"N", "America/Phoenix", "MST", "Mountain Standard Time", "MST", "Mountain Standard Time",
|
||||
"N", "America/Denver", "MST", "Mountain Standard Time", "MDT", "Mountain Daylight Time",
|
||||
"N", "America/Chicago", "CST", "Central Standard Time", "CDT", "Central Daylight Time",
|
||||
"N", "America/Indianapolis", "EST", "Eastern Standard Time", "EST", "Eastern Standard Time",
|
||||
"N", "America/Montreal", "EST", "Eastern Standard Time", "EDT", "Eastern Daylight Time",
|
||||
"N", "America/Toronto", "EST", "Eastern Standard Time", "EDT", "Eastern Daylight Time",
|
||||
"N", "America/New_York", "EST", "Eastern Standard Time", "EDT", "Eastern Daylight Time",
|
||||
"S", "America/Manaus", "GMT-04:00", "Amazon Standard Time", "GMT-04:00", "Amazon Standard Time",
|
||||
"S", "America/Campo_Grande", "GMT-04:00", "Amazon Standard Time", "GMT-03:00", "Amazon Summer Time",
|
||||
"S", "America/Bahia", "GMT-03:00", "Brasilia Standard Time", "GMT-02:00", "Brasilia Summer Time",
|
||||
"N", "America/Halifax", "AST", "Atlantic Standard Time", "ADT", "Atlantic Daylight Time",
|
||||
"N", "GMT", "GMT", "Greenwich Mean Time", "GMT", "Greenwich Mean Time",
|
||||
"N", "Europe/London", "GMT", "Greenwich Mean Time", "BST", "British Summer Time",
|
||||
"N", "Europe/Paris", "CET", "Central European Standard Time", "CEST", "Central European Summer Time",
|
||||
"N", "WET", "WET", "Western European Standard Time", "WEST", "Western European Summer Time",
|
||||
"N", "Europe/Berlin", "CET", "Central European Standard Time", "CEST", "Central European Summer Time",
|
||||
"N", "Asia/Jerusalem", "IST", "Israel Standard Time", "IDT", "Israel Daylight Time",
|
||||
"N", "Europe/Helsinki", "EET", "Eastern European Standard Time", "EEST", "Eastern European Summer Time",
|
||||
"N", "Africa/Cairo", "EET", "Eastern European Standard Time", "EEST", "Eastern European Summer Time",
|
||||
"N", "Europe/Moscow", "MSK", "Moscow Standard Time", "MSK", "Moscow Summer Time",
|
||||
"N", "Asia/Omsk", "GMT+06:00", "Omsk Standard Time", "GMT+07:00", "Omsk Summer Time",
|
||||
"N", "Asia/Shanghai", "CST", "China Standard Time", "CST", "China Standard Time",
|
||||
"N", "Asia/Tokyo", "JST", "Japan Standard Time", "JST", "Japan Standard Time",
|
||||
"N", "Japan", "JST", "Japan Standard Time", "JST", "Japan Standard Time",
|
||||
"N", "Asia/Seoul", "KST", "Korean Standard Time", "KST", "Korean Standard Time",
|
||||
"N", "ROK", "KST", "Korean Standard Time", "KST", "Korean Standard Time",
|
||||
"S", "Australia/Darwin", "ACST", "Australian Central Standard Time",
|
||||
"ACST", "Australian Central Standard Time",
|
||||
"S", "Australia/Adelaide", "ACST", "Australian Central Standard Time",
|
||||
"ACDT", "Australian Central Daylight Time",
|
||||
"S", "Australia/Broken_Hill", "ACST", "Australian Central Standard Time",
|
||||
"ACDT", "Australian Central Daylight Time",
|
||||
"S", "Australia/Hobart", "AEST", "Australian Eastern Standard Time",
|
||||
"AEDT", "Australian Eastern Daylight Time",
|
||||
"S", "Australia/Brisbane", "AEST", "Australian Eastern Standard Time",
|
||||
"AEST", "Australian Eastern Standard Time",
|
||||
"S", "Australia/Sydney", "AEST", "Australian Eastern Standard Time",
|
||||
"AEDT", "Australian Eastern Daylight Time",
|
||||
"N", "Pacific/Guam", "ChST", "Chamorro Standard Time",
|
||||
"ChST", "Chamorro Standard Time",
|
||||
"N", "Pacific/Saipan", "ChST", "Chamorro Standard Time",
|
||||
"ChST", "Chamorro Standard Time",
|
||||
};
|
||||
|
||||
@Test
|
||||
public void Test4112924() {
|
||||
SimpleDateFormat lfmt = new SimpleDateFormat("zzzz");
|
||||
SimpleDateFormat sfmt = new SimpleDateFormat("z");
|
||||
|
||||
GregorianCalendar june = new GregorianCalendar(2000, Calendar.JUNE, 21);
|
||||
GregorianCalendar december = new GregorianCalendar(2000, Calendar.DECEMBER, 21);
|
||||
|
||||
int count = data.length;
|
||||
for (int i = 0; i < count; i++) {
|
||||
GregorianCalendar sol1, sol2;
|
||||
|
||||
// check hemisphere
|
||||
if ("N".equals(data[i++])) {
|
||||
sol1 = december;
|
||||
sol2 = june;
|
||||
} else {
|
||||
sol1 = june;
|
||||
sol2 = december;
|
||||
}
|
||||
|
||||
TimeZone tz = TimeZone.getTimeZone(data[i++]);
|
||||
lfmt.setTimeZone(tz);
|
||||
sfmt.setTimeZone(tz);
|
||||
|
||||
System.out.println(tz.getID() + ": " + sfmt.format(sol1.getTime()) + ", " + lfmt.format(sol1.getTime()));
|
||||
System.out.println(tz.getID() + ": " + sfmt.format(sol2.getTime()) + ", " + lfmt.format(sol2.getTime()));
|
||||
String s = sfmt.format(sol1.getTime());
|
||||
if (!data[i].equals(s)) {
|
||||
fail(tz.getID() + ": wrong short name: \"" + s + "\" (expected \"" + data[i] + "\")");
|
||||
}
|
||||
s = lfmt.format(sol1.getTime());
|
||||
if (!data[++i].equals(s)) {
|
||||
fail(tz.getID() + ": wrong long name: \"" + s + "\" (expected \"" + data[i] + "\")");
|
||||
}
|
||||
s = sfmt.format(sol2.getTime());
|
||||
if (!data[++i].equals(s)) {
|
||||
fail(tz.getID() + ": wrong short name: \"" + s + "\" (expected \"" + data[i] + "\")");
|
||||
}
|
||||
s = lfmt.format(sol2.getTime());
|
||||
if (!data[++i].equals(s)) {
|
||||
fail(tz.getID() + ": wrong long name: \"" + s + "\" (expected \"" + data[i] + "\")");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
82
test/jdk/java/text/Format/DateFormat/ToStringTest.java
Normal file
82
test/jdk/java/text/Format/DateFormat/ToStringTest.java
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
* Copyright (c) 2024, 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 8321545
|
||||
* @library /java/text/testlib
|
||||
* @summary Ensure value returned by overridden toString method is as expected
|
||||
* @run junit ToStringTest
|
||||
*/
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.text.DateFormat;
|
||||
import java.text.DateFormatSymbols;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class ToStringTest {
|
||||
|
||||
// Check a normal expected value
|
||||
@Test
|
||||
public void normalValueTest() {
|
||||
String expectedStr =
|
||||
"SimpleDateFormat [locale: \"English (Canada)\", pattern: \"MMM d, y\"]\n";
|
||||
var s = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.CANADA);
|
||||
assertEquals(expectedStr, s.toString());
|
||||
}
|
||||
|
||||
// Check an odd value. SimpleDateFormat constructor that takes DFS, will use
|
||||
// the default locale, not the locale from the DFS.
|
||||
@Test
|
||||
public void oddValueTest() {
|
||||
String expectedStr =
|
||||
"SimpleDateFormat [locale: \"" + Locale.getDefault().getDisplayName() + "\", pattern: \"MMM d, y\"]\n";
|
||||
var s = new SimpleDateFormat("MMM d, y", new DateFormatSymbols(Locale.JAPAN));
|
||||
assertEquals(expectedStr, s.toString());
|
||||
}
|
||||
|
||||
|
||||
// Check the expected value when the locale is null. This is only possible
|
||||
// via an older SimpleDateFormat that was deserialized. The current constructor
|
||||
// will throw NPE if locale is null.
|
||||
@Test
|
||||
public void nullLocaleTest() {
|
||||
String expectedStr =
|
||||
"SimpleDateFormat [locale: null, pattern: \"yyyy.MM.dd E hh.mm.ss zzz\"]\n";
|
||||
// Borrowed from DateFormatSymbolsSerializationTest
|
||||
SimpleDateFormat s;
|
||||
try (InputStream is = HexDumpReader.getStreamFromHexDump("SDFserialized.ser.txt");
|
||||
ObjectInputStream iStream = new ObjectInputStream(is)) {
|
||||
s = (SimpleDateFormat)iStream.readObject();
|
||||
assertEquals(expectedStr, s.toString());
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error building stream from deserialized simple date format");
|
||||
}
|
||||
}
|
||||
}
|
||||
174
test/jdk/java/text/Format/DateFormat/WeekDateTest.java
Normal file
174
test/jdk/java/text/Format/DateFormat/WeekDateTest.java
Normal file
|
|
@ -0,0 +1,174 @@
|
|||
/*
|
||||
* Copyright (c) 2010, 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 4267450
|
||||
* @summary Unit test for week date support
|
||||
*/
|
||||
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import static java.util.GregorianCalendar.*;
|
||||
|
||||
public class WeekDateTest {
|
||||
static SimpleDateFormat ymdFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
|
||||
static SimpleDateFormat ywdFormat = new SimpleDateFormat("YYYY-'W'ww-u", Locale.US);
|
||||
static {
|
||||
ymdFormat.setCalendar(newCalendar());
|
||||
ywdFormat.setCalendar(newCalendar());
|
||||
}
|
||||
|
||||
// Round-trip Data
|
||||
static final String[][] roundTripData = {
|
||||
{ "2005-01-01", "2004-W53-6" },
|
||||
{ "2005-01-02", "2004-W53-7" },
|
||||
{ "2005-12-31", "2005-W52-6" },
|
||||
{ "2007-01-01", "2007-W01-1" },
|
||||
{ "2007-12-30", "2007-W52-7" },
|
||||
{ "2007-12-31", "2008-W01-1" },
|
||||
{ "2008-01-01", "2008-W01-2" },
|
||||
{ "2008-12-29", "2009-W01-1" },
|
||||
{ "2008-12-31", "2009-W01-3" },
|
||||
{ "2009-01-01", "2009-W01-4" },
|
||||
{ "2009-12-31", "2009-W53-4" },
|
||||
{ "2010-01-03", "2009-W53-7" },
|
||||
{ "2009-12-31", "2009-W53-4" },
|
||||
{ "2010-01-01", "2009-W53-5" },
|
||||
{ "2010-01-02", "2009-W53-6" },
|
||||
{ "2010-01-03", "2009-W53-7" },
|
||||
{ "2008-12-28", "2008-W52-7" },
|
||||
{ "2008-12-29", "2009-W01-1" },
|
||||
{ "2008-12-30", "2009-W01-2" },
|
||||
{ "2008-12-31", "2009-W01-3" },
|
||||
{ "2009-01-01", "2009-W01-4" },
|
||||
{ "2009-01-01", "2009-W01-4" },
|
||||
};
|
||||
|
||||
// Data for leniency test
|
||||
static final String[][] leniencyData = {
|
||||
{ "2008-12-28", "2009-W01-0" },
|
||||
{ "2010-01-04", "2009-W53-8" },
|
||||
{ "2008-12-29", "2008-W53-1" },
|
||||
};
|
||||
|
||||
static final String[] invalidData = {
|
||||
"2010-W00-1",
|
||||
"2010-W55-1",
|
||||
"2010-W03-0",
|
||||
"2010-W04-8",
|
||||
"2010-W04-19"
|
||||
};
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
formatTest(roundTripData);
|
||||
parseTest(roundTripData);
|
||||
parseTest(leniencyData);
|
||||
nonLenientTest(invalidData);
|
||||
noWeekDateSupport();
|
||||
}
|
||||
|
||||
private static void formatTest(String[][] data) throws Exception {
|
||||
for (String[] dates : data) {
|
||||
String regularDate = dates[0];
|
||||
String weekDate = dates[1];
|
||||
Date date = null;
|
||||
date = ymdFormat.parse(regularDate);
|
||||
String s = ywdFormat.format(date);
|
||||
if (!s.equals(weekDate)) {
|
||||
throw new RuntimeException("format: got="+s+", expected="+weekDate);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void parseTest(String[][] data) throws Exception {
|
||||
for (String[] dates : data) {
|
||||
String regularDate = dates[0];
|
||||
String weekDate = dates[1];
|
||||
Date date1 = null, date2 = null;
|
||||
date1 = ymdFormat.parse(regularDate);
|
||||
date2 = ywdFormat.parse(weekDate);
|
||||
if (!date1.equals(date2)) {
|
||||
System.err.println(regularDate + ": date1 = " + date1);
|
||||
System.err.println(weekDate + ": date2 = " + date2);
|
||||
throw new RuntimeException("parse: date1 != date2");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Non-lenient mode test
|
||||
private static void nonLenientTest(String[] data) {
|
||||
ywdFormat.setLenient(false);
|
||||
for (String date : data) {
|
||||
try {
|
||||
Date d = ywdFormat.parse(date);
|
||||
throw new RuntimeException("No ParseException thrown with " + date);
|
||||
} catch (ParseException e) {
|
||||
// OK
|
||||
}
|
||||
}
|
||||
ywdFormat.setLenient(true);
|
||||
}
|
||||
|
||||
|
||||
private static void noWeekDateSupport() throws Exception {
|
||||
// Tests with Japanese Imperial Calendar that doesn't support week dates.
|
||||
Calendar jcal = Calendar.getInstance(TimeZone.getTimeZone("GMT"),
|
||||
Locale.of("ja", "JP", "JP"));
|
||||
|
||||
String format = "2-W01-2"; // 2019-12-31 == R1-12-31
|
||||
int expectedYear = 2019;
|
||||
// Check the current era, Heisei or Reiwa
|
||||
if (System.currentTimeMillis() < 1556668800000L) {
|
||||
format = "21-W01-3"; // 2008-12-31 == H20-12-31
|
||||
expectedYear = 2008;
|
||||
}
|
||||
jcal.setFirstDayOfWeek(MONDAY);
|
||||
jcal.setMinimalDaysInFirstWeek(4);
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("Y-'W'ww-u");
|
||||
sdf.setCalendar(jcal);
|
||||
Date d = sdf.parse(format);
|
||||
GregorianCalendar gcal = newCalendar();
|
||||
gcal.setTime(d);
|
||||
if (gcal.get(YEAR) != expectedYear
|
||||
|| gcal.get(MONTH) != DECEMBER
|
||||
|| gcal.get(DAY_OF_MONTH) != 31) {
|
||||
String s = String.format("noWeekDateSupport: got %04d-%02d-%02d, expected %4d-12-31%n",
|
||||
gcal.get(YEAR),
|
||||
gcal.get(MONTH)+1,
|
||||
gcal.get(DAY_OF_MONTH),
|
||||
expectedYear);
|
||||
throw new RuntimeException(s);
|
||||
}
|
||||
}
|
||||
|
||||
private static GregorianCalendar newCalendar() {
|
||||
// Use GMT to avoid any surprises related DST transitions.
|
||||
GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
|
||||
// Setup the ISO 8601 compatible parameters
|
||||
cal.setFirstDayOfWeek(MONDAY);
|
||||
cal.setMinimalDaysInFirstWeek(4);
|
||||
return cal;
|
||||
}
|
||||
}
|
||||
71
test/jdk/java/text/Format/DateFormat/bug4097450.java
Normal file
71
test/jdk/java/text/Format/DateFormat/bug4097450.java
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
/*
|
||||
* Copyright (c) 1998, 2016, 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 4097450
|
||||
*/
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
public class bug4097450
|
||||
{
|
||||
public static void main(String args[])
|
||||
{
|
||||
//
|
||||
// Date parse requiring 4 digit year.
|
||||
//
|
||||
String[] dstring = {"97","1997", "97","1997","01","2001", "01","2001"
|
||||
, "1",
|
||||
"1","11", "11","111", "111"};
|
||||
String[] dformat = {"yy", "yy","yyyy","yyyy","yy", "yy","yyyy","yyyy"
|
||||
,
|
||||
"yy","yyyy","yy","yyyy", "yy","yyyy"};
|
||||
boolean[] dresult = {true, false, false, true,true, false, false, true
|
||||
,false,
|
||||
false,true, false,false, false};
|
||||
SimpleDateFormat formatter;
|
||||
SimpleDateFormat resultFormatter = new SimpleDateFormat("yyyy");
|
||||
|
||||
System.out.println("Format\tSource\tResult");
|
||||
System.out.println("-------\t-------\t-------");
|
||||
for (int i = 0; i < dstring.length; i++)
|
||||
{
|
||||
System.out.print(dformat[i] + "\t" + dstring[i] + "\t");
|
||||
formatter = new SimpleDateFormat(dformat[i]);
|
||||
try {
|
||||
System.out.print(resultFormatter.format(formatter.parse(dstring[
|
||||
i])));
|
||||
//if ( !dresult[i] ) System.out.print(" <-- error!");
|
||||
}
|
||||
catch (ParseException exception) {
|
||||
//if ( dresult[i] ) System.out.print(" <-- error!");
|
||||
System.out.print("exception --> " + exception);
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
41
test/jdk/java/text/Format/DateFormat/bug4099975.java
Normal file
41
test/jdk/java/text/Format/DateFormat/bug4099975.java
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* Copyright (c) 1998, 2016, 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 4099975
|
||||
*/
|
||||
|
||||
import java.text.*;
|
||||
import java.util.Date;
|
||||
public class bug4099975 {
|
||||
public static void main (String args[]){
|
||||
DateFormatSymbols symbols = new DateFormatSymbols();
|
||||
SimpleDateFormat df = new SimpleDateFormat("E hh:mm", symbols);
|
||||
System.out.println(df.toLocalizedPattern());
|
||||
symbols.setLocalPatternChars("abcdefghijklmonpqr"); // change value of field
|
||||
System.out.println(df.toLocalizedPattern());
|
||||
|
||||
}
|
||||
}
|
||||
84
test/jdk/java/text/Format/DateFormat/bug4117335.java
Normal file
84
test/jdk/java/text/Format/DateFormat/bug4117335.java
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
/*
|
||||
* Copyright (c) 1998, 2016, 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 4117335 4432617
|
||||
* @modules jdk.localedata
|
||||
*/
|
||||
|
||||
import java.text.DateFormatSymbols ;
|
||||
import java.util.Locale;
|
||||
|
||||
public class bug4117335 {
|
||||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
DateFormatSymbols symbols = new DateFormatSymbols(Locale.JAPAN);
|
||||
String[] eras = symbols.getEras();
|
||||
System.out.println("BC = " + eras[0]);
|
||||
if (!eras[0].equals(bc)) {
|
||||
System.out.println("*** Should have been " + bc);
|
||||
throw new Exception("Error in BC");
|
||||
}
|
||||
System.out.println("AD = " + eras[1]);
|
||||
if (!eras[1].equals(ad)) {
|
||||
System.out.println("*** Should have been " + ad);
|
||||
throw new Exception("Error in AD");
|
||||
}
|
||||
String[][] zones = symbols.getZoneStrings();
|
||||
for (int i = 0; i < zones.length; i++) {
|
||||
if (!"Asia/Tokyo".equals(zones[i][0])) {
|
||||
continue;
|
||||
}
|
||||
System.out.println("Long zone name = " + zones[i][1]);
|
||||
if (!zones[i][1].equals(jstLong)) {
|
||||
System.out.println("*** Should have been " + jstLong);
|
||||
throw new Exception("Error in long TZ name");
|
||||
}
|
||||
System.out.println("Short zone name = " + zones[i][2]);
|
||||
if (!zones[i][2].equals(jstShort)) {
|
||||
System.out.println("*** Should have been " + jstShort);
|
||||
throw new Exception("Error in short TZ name");
|
||||
}
|
||||
System.out.println("Long zone name = " + zones[i][3]);
|
||||
if (!zones[i][3].equals(jdtLong)) {
|
||||
System.out.println("*** Should have been " + jdtLong);
|
||||
throw new Exception("Error in long TZ name");
|
||||
}
|
||||
System.out.println("SHORT zone name = " + zones[i][4]);
|
||||
if (!zones[i][4].equals(jdtShort)) {
|
||||
System.out.println("*** Should have been " + jdtShort);
|
||||
throw new Exception("Error in short TZ name");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static final String bc = "\u7d00\u5143\u524d";
|
||||
static final String ad = "\u897f\u66a6";
|
||||
static final String jstLong = "\u65e5\u672c\u6a19\u6e96\u6642";
|
||||
static final String jstShort = "JST";
|
||||
static final String jdtLong = "\u65e5\u672c\u590f\u6642\u9593";
|
||||
static final String jdtShort = "JDT";
|
||||
}
|
||||
80
test/jdk/java/text/Format/DateFormat/bug4358730.java
Normal file
80
test/jdk/java/text/Format/DateFormat/bug4358730.java
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
/*
|
||||
* Copyright (c) 2000, 2025, 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.
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
import java.text.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @bug 4358730 8347841
|
||||
* @summary test that confirms Zero-Padding on year.
|
||||
* @run junit bug4358730
|
||||
*/
|
||||
|
||||
public class bug4358730 {
|
||||
|
||||
String[] patterns = {"y", "yy", "yyy", "yyyy", "yyyyy"};
|
||||
String[][] data = {
|
||||
/* 2 A.D. */ {"2", "02", "002", "0002", "00002"},
|
||||
/* 20 A.D. */ {"20", "20", "020", "0020", "00020"},
|
||||
/* 200 A.D. */ {"200", "00", "200", "0200", "00200"},
|
||||
/* 2000 A.D. */ {"2000", "00", "2000", "2000", "02000"},
|
||||
};
|
||||
int[] year = {2, 20, 200, 2000};
|
||||
|
||||
int datasize = data.length;
|
||||
int nPatterns = data[0].length;
|
||||
|
||||
@Test
|
||||
public void Test4358730() {
|
||||
TimeZone saveZone = TimeZone.getDefault();
|
||||
Locale saveLocale = Locale.getDefault();
|
||||
|
||||
try {
|
||||
TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
|
||||
Locale.setDefault(Locale.US);
|
||||
SimpleDateFormat sdf = new SimpleDateFormat();
|
||||
|
||||
for (int i = 0; i < datasize; i++) {
|
||||
@SuppressWarnings("deprecation")
|
||||
Date d = new Date(year[i]-1900, 10, 15);
|
||||
for (int j = 0; j < nPatterns; j++) {
|
||||
sdf.applyPattern(patterns[j]);
|
||||
if (!data[i][j].equals(sdf.format(d))) {
|
||||
fail("Invalid format : " + sdf.format(d) +
|
||||
", expected : " + data[i][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
finally {
|
||||
TimeZone.setDefault(saveZone);
|
||||
Locale.setDefault(saveLocale);
|
||||
}
|
||||
}
|
||||
}
|
||||
61
test/jdk/java/text/Format/DateFormat/bug6271375.java
Normal file
61
test/jdk/java/text/Format/DateFormat/bug6271375.java
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
* Copyright (c) 2005, 2016, 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 6271375 7059546
|
||||
* @summary Make sure DateFormatSymbols serialization works
|
||||
* correctly for 'zoneStrings' field
|
||||
*/
|
||||
|
||||
import java.util.*;
|
||||
import java.text.*;
|
||||
import java.io.*;
|
||||
|
||||
public class bug6271375 {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
DateFormatSymbols dfsSrc = DateFormatSymbols.getInstance();
|
||||
|
||||
try (FileOutputStream fos = new FileOutputStream("dfs.ser");
|
||||
ObjectOutputStream oStream = new ObjectOutputStream(fos)) {
|
||||
oStream.writeObject(dfsSrc);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("An exception is thrown.", e);
|
||||
}
|
||||
|
||||
try (FileInputStream fis = new FileInputStream("dfs.ser");
|
||||
ObjectInputStream iStream = new ObjectInputStream(fis)) {
|
||||
DateFormatSymbols dfsDest = (DateFormatSymbols)iStream.readObject();
|
||||
|
||||
String[][] zoneStringsSrc = dfsSrc.getZoneStrings();
|
||||
String[][] zoneStringsDest = dfsDest.getZoneStrings();
|
||||
|
||||
if (!Arrays.deepEquals(zoneStringsSrc, zoneStringsDest)) {
|
||||
throw new RuntimeException("src and dest zone strings are not equal");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("An exception is thrown.", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
81
test/jdk/java/text/Format/DateFormat/bug6317072.java
Normal file
81
test/jdk/java/text/Format/DateFormat/bug6317072.java
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
/*
|
||||
* Copyright (c) 2005, 2016, 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 6317072
|
||||
* @summary Make sure NPE is thrown with "null" argumemnts in the
|
||||
* SimpleDateFormat constructors.
|
||||
*/
|
||||
|
||||
import java.util.*;
|
||||
import java.text.*;
|
||||
import java.io.*;
|
||||
|
||||
public class bug6317072 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
try {
|
||||
new SimpleDateFormat("yy", (Locale)null);
|
||||
throw new RuntimeException("should thrown a NullPointerException");
|
||||
} catch (NullPointerException e) {
|
||||
}
|
||||
|
||||
try {
|
||||
new SimpleDateFormat((String)null, Locale.getDefault());
|
||||
throw new RuntimeException("should thrown a NullPointerException");
|
||||
} catch (NullPointerException e) {
|
||||
}
|
||||
|
||||
try {
|
||||
new SimpleDateFormat("yy", (DateFormatSymbols)null);
|
||||
throw new RuntimeException("should thrown a NullPointerException");
|
||||
} catch (NullPointerException e) {
|
||||
}
|
||||
|
||||
try {
|
||||
new SimpleDateFormat((String)null, DateFormatSymbols.getInstance());
|
||||
throw new RuntimeException("should thrown a NullPointerException");
|
||||
} catch (NullPointerException e) {
|
||||
}
|
||||
|
||||
try {
|
||||
DateFormat.getTimeInstance(DateFormat.FULL, null);
|
||||
throw new RuntimeException("should thrown a NullPointerException");
|
||||
} catch (NullPointerException e) {
|
||||
}
|
||||
|
||||
try {
|
||||
DateFormat.getDateInstance(DateFormat.FULL, null);
|
||||
throw new RuntimeException("should thrown a NullPointerException");
|
||||
} catch (NullPointerException e) {
|
||||
}
|
||||
|
||||
try {
|
||||
DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, null);
|
||||
throw new RuntimeException("should thrown a NullPointerException");
|
||||
} catch (NullPointerException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
50
test/jdk/java/text/Format/DateFormat/bug6412896.java
Normal file
50
test/jdk/java/text/Format/DateFormat/bug6412896.java
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
* Copyright (c) 2006, 2016, 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 6412896
|
||||
* @summary Make sure that an IllegalArgumentException is thrown
|
||||
* if the length of any row in zoneStrings array is less than 5
|
||||
*/
|
||||
|
||||
import java.text.*;
|
||||
|
||||
public class bug6412896 {
|
||||
|
||||
static final String[][] zoneOK = {{"America/Los_Angeles", "Pacific Standard Time", "PST", "Pacific Daylight Time", "PDT"}};
|
||||
static final String[][] zoneNG = {{"America/Los_Angeles", "Pacific Standard Time", "PST", "Pacific Daylight Time"}};
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
DateFormatSymbols dfs = DateFormatSymbols.getInstance();
|
||||
|
||||
dfs.setZoneStrings(zoneOK);
|
||||
|
||||
try {
|
||||
dfs.setZoneStrings(zoneNG);
|
||||
throw new RuntimeException("should throw an IllegalArgumentException");
|
||||
} catch (IllegalArgumentException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
* Copyright Amazon.com Inc. 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.
|
||||
*/
|
||||
|
||||
package java.text;
|
||||
|
||||
/**
|
||||
* Test validated that CalendarBuilder.toString does not throw an ArrayIndexOutOfBoundException.
|
||||
*/
|
||||
|
||||
import java.util.Calendar;
|
||||
|
||||
public class CalendarBuilderTest {
|
||||
public static void testCalendarBuilderToString() {
|
||||
CalendarBuilder calendarBuilder = new CalendarBuilder();
|
||||
calendarBuilder.set(Calendar.YEAR, 2020);
|
||||
calendarBuilder.toString();
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue