1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
|
/*
* Copyright (c) 1997, 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 4052223 4089987 4469904 4326988 4486735 8008577 8045998 8140571
* @summary test DateFormat and SimpleDateFormat.
* @library /java/text/testlib
* @modules jdk.localedata
* @run main/othervm -Djava.locale.providers=COMPAT,SPI DateFormatTest
*/
import java.util.*;
import java.text.*;
import static java.util.GregorianCalendar.*;
public class DateFormatTest extends IntlTest
{
public static void main(String[] args) throws Exception {
Locale reservedLocale = Locale.getDefault();
try {
Locale.setDefault(Locale.US);
new DateFormatTest().run(args);
} finally {
// restore the reserved locale
Locale.setDefault(reservedLocale);
}
}
// Test 4 digit year parsing with pattern "yy"
@SuppressWarnings("deprecation")
public void TestYearParsing()
{
String str = "7/Sep/2001";
Date exp = new Date(2001-1900, SEPTEMBER, 7);
String pat = "d/MMM/yy";
SimpleDateFormat sdf = new SimpleDateFormat(pat, Locale.US);
try {
Date d = sdf.parse(str);
logln(str + " parses with " + pat + " to " + d);
if (d.getTime() != exp.getTime()) {
errln("FAIL: Expected " + exp);
}
}
catch (ParseException e) {
errln(str + " parse fails with " + pat);
}
}
// Test written by Wally Wedel and emailed to me.
public void TestWallyWedel()
{
/*
* Instantiate a TimeZone so we can get the ids.
*/
TimeZone tz = new SimpleTimeZone(7,"");
/*
* Computational variables.
*/
int offset, hours, minutes;
/*
* Instantiate a SimpleDateFormat set up to produce a full time
zone name.
*/
SimpleDateFormat sdf = new SimpleDateFormat("zzzz");
/*
* A String array for the time zone ids.
*/
String[] ids = TimeZone.getAvailableIDs();
/*
* How many ids do we have?
*/
logln("Time Zone IDs size: " + ids.length);
/*
* Column headings (sort of)
*/
logln("Ordinal ID offset(h:m) name");
/*
* Loop through the tzs.
*/
Date today = new Date();
Calendar cal = Calendar.getInstance();
for (int i = 0; i < ids.length; i++) {
// logln(i + " " + ids[i]);
TimeZone ttz = TimeZone.getTimeZone(ids[i]);
// offset = ttz.getRawOffset();
cal.setTimeZone(ttz);
cal.setTime(today);
offset = cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET);
// logln(i + " " + ids[i] + " offset " + offset);
char sign = '+';
if (offset < 0) { sign = '-'; offset = -offset; }
hours = offset/3600000;
minutes = (offset%3600000)/60000;
String dstOffset = "" + sign + (hours < 10 ? "0" : "") +
hours + ':' + (minutes < 10 ? "0" : "") + minutes;
/*
* Instantiate a date so we can display the time zone name.
*/
sdf.setTimeZone(ttz);
/*
* Format the output.
*/
StringBuffer tzS = new StringBuffer();
sdf.format(today,tzS, new FieldPosition(0));
String fmtOffset = tzS.toString();
String fmtDstOffset = null;
if (fmtOffset.startsWith("GMT"))
{
fmtDstOffset = fmtOffset.substring(3);
}
/*
* Show our result.
*/
boolean ok = fmtDstOffset == null || fmtDstOffset.equals(dstOffset);
if (ok)
{
logln(i + " " + ids[i] + " " + dstOffset +
" " + fmtOffset +
(fmtDstOffset != null ? " ok" : " ?"));
}
else
{
errln(i + " " + ids[i] + " " + dstOffset +
" " + fmtOffset + " *** FAIL ***");
}
}
}
// Test equals
public void TestEquals()
{
DateFormat fmtA = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.FULL);
DateFormat fmtB = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.FULL);
if (!fmtA.equals(fmtB)) {
errln("FAIL");
}
}
// Check out some specific parsing problem
@SuppressWarnings("deprecation")
public void TestTwoDigitYearDSTParse()
{
SimpleDateFormat fullFmt =
new SimpleDateFormat("EEE MMM dd HH:mm:ss.SSS zzz yyyy G");
//DateFormat fmt = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.FULL,
// Locale.ENGLISH);
SimpleDateFormat fmt = new SimpleDateFormat("dd-MMM-yy h:mm:ss 'o''clock' a z",
Locale.ENGLISH);
//Date date = new Date(2004-1900, Calendar.APRIL, 3, 2, 20, 47);
//logln(fmt.format(date)); // This shows what the current locale format is
//logln(((SimpleDateFormat)fmt).toPattern());
TimeZone save = TimeZone.getDefault();
TimeZone PST = TimeZone.getTimeZone("PST");
String s = "03-Apr-04 2:20:47 o'clock AM PST";
int hour = 2;
try {
TimeZone.setDefault(PST);
Date d = fmt.parse(s);
logln(s + " P> " + fullFmt.format(d));
if (d.getHours() != hour) {
errln("FAIL: Should parse to hour " + hour);
}
}
catch (ParseException e) { errln("FAIL: " + e.getMessage()); }
finally {
TimeZone.setDefault(save);
}
}
static String escape(String s)
{
StringBuilder buf = new StringBuilder();
for (int i=0; i<s.length(); ++i)
{
char c = s.charAt(i);
if (c <= (char)0x7F) {
buf.append(c);
} else {
buf.append("\\u");
buf.append(Integer.toHexString((c & 0xF000) >> 12));
buf.append(Integer.toHexString((c & 0x0F00) >> 8));
buf.append(Integer.toHexString((c & 0x00F0) >> 4));
buf.append(Integer.toHexString(c & 0x000F));
}
}
return buf.toString();
}
// Test field position return values
static String fieldNames[] = {
"ERA_FIELD", "YEAR_FIELD", "MONTH_FIELD",
"WEEK_OF_YEAR_FIELD", "WEEK_OF_MONTH_FIELD", "DATE_FIELD",
"DAY_OF_YEAR_FIELD", "DAY_OF_WEEK_FIELD", "DAY_OF_WEEK_IN_MONTH_FIELD",
"AM_PM_FIELD", "HOUR0_FIELD", "HOUR1_FIELD",
"HOUR_OF_DAY0_FIELD", "HOUR_OF_DAY1_FIELD",
"MINUTE_FIELD", "SECOND_FIELD",
"MILLISECOND_FIELD", "TIMEZONE_FIELD",
};
static int fieldIDs[] = {
DateFormat.ERA_FIELD, DateFormat.YEAR_FIELD, DateFormat.MONTH_FIELD,
DateFormat.WEEK_OF_YEAR_FIELD, DateFormat.WEEK_OF_MONTH_FIELD, DateFormat.DATE_FIELD,
DateFormat.DAY_OF_YEAR_FIELD, DateFormat.DAY_OF_WEEK_FIELD, DateFormat.DAY_OF_WEEK_IN_MONTH_FIELD,
DateFormat.AM_PM_FIELD, DateFormat.HOUR0_FIELD, DateFormat.HOUR1_FIELD,
DateFormat.HOUR_OF_DAY0_FIELD, DateFormat.HOUR_OF_DAY1_FIELD,
DateFormat.MINUTE_FIELD, DateFormat.SECOND_FIELD,
DateFormat.MILLISECOND_FIELD, DateFormat.TIMEZONE_FIELD,
};
/**
* Bug 4089987
*/
public void TestFieldPosition()
{
DateFormat[] dateFormats = {
DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL,
Locale.US),
DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL,Locale.FRANCE),
new SimpleDateFormat("G, y, M, d, k, H, m, s, S, E, D, F, w, W, a, h, K, z"),
new SimpleDateFormat("G, yy, M, d, k, H, m, s, S, E, D, F, w, W, a, h, K, z"),
new SimpleDateFormat( "GGGG, yyyy, MMMM, dddd, kkkk, HHHH, mmmm, ssss, " +
"SSSS, EEEE, DDDD, " +
"FFFF, wwww, WWWW, aaaa, hhhh, KKKK, zzzz")
};
String[] expected =
{
"", "1997", "August", "", "", "13", "", "Wednesday", "",
"PM", "", "2", "", "", "34", "12", "", "PDT",
"", "1997", "ao\u00FBt", "", "", "13", "", "mercredi", "", "",
"", "", "14", "", "34", "", "", "PDT" /*"GMT-07:00"*/,
"AD", "1997", "8", "33", "3", "13", "225", "Wed", "2", "PM",
"2", "2", "14", "14", "34", "12", "513", "PDT",
"AD", "97", "8", "33", "3", "13", "225", "Wed", "2", "PM",
"2", "2", "14", "14", "34", "12", "513", "PDT",
"AD", "1997", "August", "0033", "0003", "0013", "0225",
"Wednesday", "0002", "PM", "0002", "0002", "0014", "0014",
"0034", "0012", "0513", "Pacific Daylight Time",
};
Date someDate = new Date(871508052513L);
TimeZone PST = TimeZone.getTimeZone("PST");
for (int j = 0, exp = 0; j < dateFormats.length; ++j) {
DateFormat df = dateFormats[j];
if (!(df instanceof SimpleDateFormat)) {
continue;
}
df.setTimeZone(PST);
logln(" Pattern = " + ((SimpleDateFormat)df).toPattern());
logln(" Result = " + df.format(someDate));
for (int i = 0; i < fieldIDs.length; ++i)
{
String field = getFieldText(df, fieldIDs[i], someDate);
if (!field.equals(expected[exp])) {
errln("FAIL: field #" + i + " " + fieldNames[i] + " = \"" +
escape(field) + "\", expected \"" + escape(expected[exp]) + "\"");
}
++exp;
}
}
}
// get the string value for the given field for the given date
static String getFieldText(DateFormat df, int field, Date date)
{
StringBuffer buffer = new StringBuffer();
FieldPosition pos = new FieldPosition(field);
df.format(date, buffer, pos);
return buffer.toString().substring(pos.getBeginIndex(),
pos.getEndIndex());
}
// Test parsing of partial strings
@SuppressWarnings("deprecation")
public void TestPartialParse994()
{
SimpleDateFormat f = new SimpleDateFormat();
Calendar cal = new GregorianCalendar(2014 - 80, JANUARY, 1);
f.set2DigitYearStart(cal.getTime());
tryPat994(f, "yy/MM/dd HH:mm:ss", "97/01/17 10:11:42", new Date(97, 1-1, 17, 10, 11, 42));
tryPat994(f, "yy/MM/dd HH:mm:ss", "97/01/17 10:", null);
tryPat994(f, "yy/MM/dd HH:mm:ss", "97/01/17 10", null);
tryPat994(f, "yy/MM/dd HH:mm:ss", "97/01/17 ", null);
tryPat994(f, "yy/MM/dd HH:mm:ss", "97/01/17", null);
}
void tryPat994(SimpleDateFormat format, String pat, String str, Date expected)
{
logln("Pattern \"" + pat + "\" String \"" + str + "\"");
try {
format.applyPattern(pat);
Date date = format.parse(str);
String f = format.format(date);
logln(" parse(" + str + ") -> " + date.toString());
logln(" format -> " + f);
if (expected == null ||
!date.equals(expected)) {
errln("FAIL: Expected " + expected);
}
if (!f.equals(str)) {
errln("FAIL: Expected " + str);
}
}
catch(ParseException e) {
logln("ParseException: " + e.getMessage());
if (expected != null) {
errln("FAIL: Expected " + expected);
}
}
catch(Exception e) {
errln("*** Exception:");
e.printStackTrace();
}
}
// Test pattern with runs things together
public void TestRunTogetherPattern985()
{
String format = "yyyyMMddHHmmssSSS";
String now, then;
SimpleDateFormat formatter = new SimpleDateFormat(format);
Date date1 = new Date();
now = formatter.format(date1);
logln(now);
ParsePosition pos = new ParsePosition(0);
Date date2 = formatter.parse(now, pos);
if (date2 == null) {
then = "Parse stopped at " + pos.getIndex();
} else {
then = formatter.format(date2);
}
logln(then);
if (!date2.equals(date1)) {
errln("FAIL");
}
}
// Test patterns which run numbers together
@SuppressWarnings("deprecation")
public void TestRunTogetherPattern917()
{
SimpleDateFormat fmt;
String myDate;
fmt = new SimpleDateFormat( "yyyy/MM/dd" );
myDate = "1997/02/03";
_testIt917( fmt, myDate, new Date(97, 2-1, 3) );
fmt = new SimpleDateFormat( "yyyyMMdd" );
myDate = "19970304";
_testIt917( fmt, myDate, new Date(97, 3-1, 4) );
}
void _testIt917( SimpleDateFormat fmt, String str, Date expected )
{
logln( "pattern=" + fmt.toPattern() + " string=" + str );
Object o;
try {
o = fmt.parseObject( str );
} catch( ParseException e ) {
e.printStackTrace();
return;
}
logln( "Parsed object: " + o );
if (!o.equals(expected)) {
errln("FAIL: Expected " + expected);
}
String formatted = fmt.format( o );
logln( "Formatted string: " + formatted );
if (!formatted.equals(str)) {
errln("FAIL: Expected " + str);
}
}
// Test Czech month formatting -- this can cause a problem because the June and
// July month names share a common prefix.
@SuppressWarnings("deprecation")
public void TestCzechMonths459()
{
// Use Czech, which has month names with shared prefixes for June and July
DateFormat fmt = DateFormat.getDateInstance(DateFormat.FULL, new Locale("cs", "", ""));
//((SimpleDateFormat)fmt).applyPattern("MMMM d yyyy");
logln("Pattern " + ((SimpleDateFormat)fmt).toPattern());
Date june = new Date(97, Calendar.JUNE, 15);
Date july = new Date(97, Calendar.JULY, 15);
String juneStr = fmt.format(june);
String julyStr = fmt.format(july);
try {
logln("format(June 15 1997) = " + juneStr);
Date d = fmt.parse(juneStr);
String s = fmt.format(d);
int month = d.getMonth();
logln(" -> parse -> " + s + " (month = " + month + ")");
if (month != JUNE) {
errln("FAIL: Month should be June");
}
logln("format(July 15 1997) = " + julyStr);
d = fmt.parse(julyStr);
s = fmt.format(d);
month = d.getMonth();
logln(" -> parse -> " + s + " (month = " + month + ")");
if (month != JULY) {
errln("FAIL: Month should be July");
}
}
catch (ParseException e) {
errln("Exception: " + e);
}
}
// Test big D (day of year) versus little d (day of month)
@SuppressWarnings("deprecation")
public void TestLetterDPattern212()
{
String dateString = "1995-040.05:01:29";
String bigD = "yyyy-DDD.hh:mm:ss";
String littleD = "yyyy-ddd.hh:mm:ss";
Date expLittleD = new Date(95, 0, 1, 5, 1, 29);
Date expBigD = new Date(expLittleD.getTime() + 39*24*3600000L); // 39 days
expLittleD = expBigD; // Expect the same, with default lenient parsing
logln( "dateString= " + dateString );
SimpleDateFormat formatter = new SimpleDateFormat(bigD);
ParsePosition pos = new ParsePosition(0);
Date myDate = formatter.parse( dateString, pos );
logln("Using " + bigD + " -> " + myDate);
if (myDate.getTime() != expBigD.getTime()) {
errln("FAIL: Expected " + expBigD + " got " + myDate);
}
formatter = new SimpleDateFormat(littleD);
pos = new ParsePosition(0);
myDate = formatter.parse( dateString, pos );
logln("Using " + littleD + " -> " + myDate);
if (myDate.getTime() != expLittleD.getTime()) {
errln("FAIL: Expected " + expLittleD + " got " + myDate);
}
}
// Test the 'G' day of year pattern
@SuppressWarnings("deprecation")
public void TestDayOfYearPattern195()
{
Date today = new Date();
Date expected = new Date(today.getYear(), today.getMonth(), today.getDate());
logln("Test Date: " + today);
SimpleDateFormat sdf =
(SimpleDateFormat)SimpleDateFormat.getDateInstance();
tryPattern(sdf, today, null, expected);
tryPattern(sdf, today, "G yyyy DDD", expected);
}
void tryPattern(SimpleDateFormat sdf, Date d, String pattern, Date expected)
{
if (pattern != null) {
sdf.applyPattern(pattern);
}
logln("pattern: " + sdf.toPattern());
String formatResult = sdf.format(d);
logln(" format -> " + formatResult);
try {
Date d2 = sdf.parse(formatResult);
logln(" parse(" + formatResult + ") -> " + d2);
if (d2.getTime() != expected.getTime()) {
errln("FAIL: Expected " + expected);
}
String format2 = sdf.format(d2);
logln(" format -> " + format2);
if (!formatResult.equals(format2)) {
errln("FAIL: Round trip drift");
}
}
catch(Exception e) {
errln("Error: " + e.getMessage());
}
}
// Test a pattern with single quotes
@SuppressWarnings("deprecation")
public void TestQuotePattern161()
{
// This pattern used to end in " zzz" but that makes this test zone-dependent
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy 'at' hh:mm:ss a zzz");
Date currentTime_1 = new Date(97, Calendar.AUGUST, 13, 10, 42, 28);
String dateString = formatter.format(currentTime_1);
String exp = "08/13/1997 at 10:42:28 AM ";
logln("format(" + currentTime_1 + ") = " + dateString);
if (!dateString.regionMatches(0, exp, 0, exp.length())) {
errln("FAIL: Expected " + exp);
}
}
// Test the parsing of bad input strings
/** Demonstrates a number of bugs in DateFormat.parse(String) where
* either StringIndexOutOfBoundsException is thrown or null is
* returned instead of ParseException. To reproduce, run this program
* and notice all the "SHOULD NOT HAPPEN" errors. Note also that the
* 1 line that should be correct is off by 100 years. (In this day
* and age, no one would assume that 1/1/00 is Jan 1 1900.)
**/
public void TestBadInput135()
{
int looks[] = { DateFormat.SHORT, DateFormat.MEDIUM,
DateFormat.LONG, DateFormat.FULL };
String strings[] = { "Mar 15", "Mar 15 1997", "asdf",
"3/1/97 1:23:", "3/1/00 1:23:45 AM" };
DateFormat full = DateFormat.getDateTimeInstance(DateFormat.LONG,
DateFormat.LONG);
String expected = "March 1, 2000 1:23:45 AM ";
for ( int i = 0; i < strings.length; ++i ){
String text = strings[i];
for ( int j = 0; j < looks.length; ++j ){
int dateLook = looks[j];
for ( int k = 0; k < looks.length; ++k ){
int timeLook = looks[k];
DateFormat df = DateFormat.getDateTimeInstance(dateLook, timeLook);
String prefix = text + ", " + dateLook + "/" + timeLook + ": ";
try {
Date when = df.parse(text);
if ( when == null ){
errln(prefix +
"SHOULD NOT HAPPEN: parse returned null.");
continue;
}
String format = full.format(when);
logln(prefix + "OK: " + format);
// Only match the start -- not the zone, which could vary
if (!format.regionMatches(0, expected, 0, expected.length())) {
errln("FAIL: Expected " + expected);
}
}
catch ( ParseException e ){
//errln(prefix + e); // This is expected.
}
catch ( StringIndexOutOfBoundsException e ){
errln(prefix + "SHOULD NOT HAPPEN: " + e);
}
}
}
}
}
final private static String parseFormats[] =
{
"MMMM d, yyyy", // january 1, 1970 or jan 1, 1970
"MMMM d yyyy", // january 1 1970 or jan 1 1970
"M/d/yy", // 1/1/70
"d MMMM, yyyy", // 1 january, 1970 or 1 jan, 1970
"d MMMM yyyy", // 1 january 1970 or 1 jan 1970
"d MMMM", // 1 january or 1 jan
"MMMM d", // january 1 or jan 1
"yyyy", // 1970
"h:mm a MMMM d, yyyy" // Date and Time
};
final private static String inputStrings[] =
{
"bogus string", null, null, null, null, null, null, null, null, null,
"April 1, 1997", "April 1, 1997", null, null, null, null, null, "April 1", null, null,
"Jan 1, 1970", "January 1, 1970", null, null, null, null, null, "January 1", null, null,
"Jan 1 2037", null, "January 1 2037", null, null, null, null, "January 1", null, null,
"1/1/70", null, null, "1/1/70", null, null, null, null, "0001", null,
"5 May 1997", null, null, null, null, "5 May 1997", "5 May", null, "0005", null,
"16 May", null, null, null, null, null, "16 May", null, "0016", null,
"April 30", null, null, null, null, null, null, "April 30", null, null,
"1998", null, null, null, null, null, null, null, "1998", null,
"1", null, null, null, null, null, null, null, "0001", null, // Bug620
"3:00 pm Jan 1, 1997", null, null, null, null, null, null, null, "0003", "3:00 PM January 1, 1997",
};
// More testing of the parsing of bad input
@SuppressWarnings("UnusedAssignment")
public void TestBadInput135a()
{
SimpleDateFormat dateParse = new SimpleDateFormat();
String s;
Date date;
int PFLENGTH = parseFormats.length;
dateParse.applyPattern("d MMMM, yyyy");
dateParse.setTimeZone(TimeZone.getDefault());
s = "not parseable";
logln("Trying to parse \"" + s + "\" with " + dateParse.toPattern());
try {
date = dateParse.parse(s);
errln("FAIL: Expected exception during parse");
} catch (Exception ex) {
logln("Exception during parse: " + ex); // This is expected
}
for (int i=0; i<inputStrings.length; i += (PFLENGTH+1))
{
ParsePosition parsePosition = new ParsePosition(0);
s = inputStrings[i];
for (int index=0; index<PFLENGTH; ++index)
{
String expected = inputStrings[i + 1 + index];
dateParse.applyPattern(parseFormats[index]);
dateParse.setTimeZone(TimeZone.getDefault());
// logln("Trying to parse \"" + s + "\" with " + dateParse.toPattern());
try {
parsePosition.setIndex(0);
date = dateParse.parse(s, parsePosition);
if (parsePosition.getIndex() != 0) {
if (date == null) {
errln("ERROR: null result with pos " +
parsePosition.getIndex() + " " +
s.substring(0, parsePosition.getIndex()) + "|" +
s.substring(parsePosition.getIndex()));
} else {
String result = dateParse.format(date);
logln("Parsed \"" + s + "\" using \"" + dateParse.toPattern() +
"\" to: " + result);
if (expected == null) {
errln("FAIL: Expected parse failure");
} else if (!expected.equals(result)) {
errln("FAIL: Expected " + expected);
}
}
} else {
// logln("Not parsed.");
if (expected != null) {
errln("FAIL: Expected " + expected);
}
}
} catch (Exception ex) {
errln("An exception was thrown during parse: " + ex);
}
}
}
}
// Test the handling of 2-digit dates
public void TestTwoDigitYear() {
SimpleDateFormat fmt = new SimpleDateFormat("M/d/yy");
// find out the expected 2-digit year values for "6/5/17" and "6/4/34"
long start = fmt.get2DigitYearStart().getTime();
Calendar cal = new Calendar.Builder().setInstant(start).build();
int startYear = cal.get(YEAR);
cal.add(YEAR, 100);
long end = cal.getTimeInMillis();
int endYear = cal.get(YEAR);
int xx17 = 0, xx34 = 0;
for (int year = startYear; year <= endYear; year++) {
int yy = year % 100;
if (yy == 17 && xx17 == 0) {
xx17 = yearValue(start, end, year, JUNE, 5);
} else if (yy == 34 && xx34 == 0) {
xx34 = yearValue(start, end, year, JUNE, 4);
}
if (xx17 != 0 && xx34 != 0) {
break;
}
}
if (xx17 == 0 || xx34 == 0) {
errln("Failed: producing expected values: 2DigitYearStart: " + new Date(start)
+ ", xx17 = " + xx17 + ", xx34 = " + xx34);
}
logln("2DigitYearStart: " + new Date(start) + ", xx17 = " + xx17 + ", xx34 = " + xx34);
parse2DigitYear(fmt, "6/5/17", new GregorianCalendar(xx17, JUNE, 5).getTime());
parse2DigitYear(fmt, "6/4/34", new GregorianCalendar(xx34, JUNE, 4).getTime());
}
private int yearValue(long start, long end, int year, int month, int dayOfMonth) {
Calendar cal = new GregorianCalendar(year, month, dayOfMonth);
long time = cal.getTimeInMillis();
return (start <= time && time < end) ? year : 0;
}
private void parse2DigitYear(SimpleDateFormat fmt, String str, Date expected) {
try {
Date d = fmt.parse(str);
logln("Parsing \"" + str + "\" with " +
fmt.toPattern() +
" => " + d.toString());
if (d.getTime() != expected.getTime()) {
errln("FAIL: Expected " + expected);
}
} catch (ParseException e) {
errln("FAIL: Got exception");
}
}
// Test behavior of DateFormat with applied time zone
public void TestDateFormatZone061()
{
Date date;
DateFormat formatter;
// 25-Mar-97 00:00:00 GMT
date = new Date( 859248000000L );
logln( "Date 1997/3/25 00:00 GMT: " + date );
formatter = new SimpleDateFormat("dd-MMM-yyyyy HH:mm", Locale.UK);
formatter.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
String temp = formatter.format( date );
logln( "Formatted in GMT to: " + temp );
/* Parse date string */
try {
Date tempDate = formatter.parse( temp );
logln( "Parsed to: " + tempDate );
if (tempDate.getTime() != date.getTime()) {
errln("FAIL: Expected " + date);
}
}
catch( Throwable t ) {
errln( "Date Formatter throws: " +
t.toString() );
}
}
// Make sure DateFormat uses the correct zone.
public void TestDateFormatZone146()
{
TimeZone saveDefault = TimeZone.getDefault();
try {
TimeZone thedefault = TimeZone.getTimeZone("GMT");
TimeZone.setDefault(thedefault);
// java.util.Locale.setDefault(new java.util.Locale("ar", "", ""));
// check to be sure... its GMT all right
TimeZone testdefault = TimeZone.getDefault();
String testtimezone = testdefault.getID();
if (testtimezone.equals("GMT")) {
logln("Test timezone = " + testtimezone);
} else {
errln("Test timezone should be GMT, not " + testtimezone);
}
// now try to use the default GMT time zone
GregorianCalendar greenwichcalendar =
new GregorianCalendar(1997, 3, 4, 23, 0);
//*****************************greenwichcalendar.setTimeZone(TimeZone.getDefault());
//greenwichcalendar.set(1997, 3, 4, 23, 0);
// try anything to set hour to 23:00 !!!
greenwichcalendar.set(Calendar.HOUR_OF_DAY, 23);
// get time
Date greenwichdate = greenwichcalendar.getTime();
// format every way
String[] DATA = {
"simple format: ", "04/04/97 23:00 GMT",
"MM/dd/yy HH:mm z",
"full format: ", "Friday, April 4, 1997 11:00:00 o'clock PM GMT",
"EEEE, MMMM d, yyyy h:mm:ss 'o''clock' a z",
"long format: ", "April 4, 1997 11:00:00 PM GMT",
"MMMM d, yyyy h:mm:ss a z",
"default format: ", "04-Apr-97 11:00:00 PM",
"dd-MMM-yy h:mm:ss a",
"short format: ", "4/4/97 11:00 PM",
"M/d/yy h:mm a",
};
for (int i=0; i<DATA.length; i+=3) {
DateFormat fmt = new SimpleDateFormat(DATA[i+2], Locale.ENGLISH);
fmt.setCalendar(greenwichcalendar);
String result = fmt.format(greenwichdate);
logln(DATA[i] + result);
if (!result.equals(DATA[i+1])) {
errln("FAIL: Expected " + DATA[i+1]
+ ", got " + result);
}
}
}
finally {
TimeZone.setDefault(saveDefault);
}
}
/* HS : Commented out for now, need to be changed not to use hardcoded results.
public void TestLocaleDateFormat() // Bug 495
{
Date testDate = new Date (97, Calendar.SEPTEMBER, 15);
DateFormat dfFrench = DateFormat.getDateTimeInstance(DateFormat.FULL,
DateFormat.FULL, Locale.FRENCH);
DateFormat dfUS = DateFormat.getDateTimeInstance(DateFormat.FULL,
DateFormat.FULL, Locale.US);
String expectedFRENCH = "lundi 15 septembre 1997 00 h 00 GMT-07:00";
String expectedUS = "Monday, September 15, 1997 12:00:00 o'clock AM PDT";
logln("Date set to : " + testDate);
String out = dfFrench.format(testDate);
logln("Date Formated with French Locale " + out);
if (!out.equals(expectedFRENCH)) errln("FAIL: Expected " + expectedFRENCH);
out = dfUS.format(testDate);
logln("Date Formated with US Locale " + out);
if (!out.equals(expectedUS)) errln("FAIL: Expected " + expectedUS);
}
*/
/**
* Bug 4056591
*/
/*
test commented out pending API-change approval
public void Test2YearStartDate() throws ParseException
{
// create a SimpleDateFormat to test with; dump out if it's not a SimpleDateFormat
DateFormat test = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);
if (!(test instanceof SimpleDateFormat)) {
errln("DateFormat.getInstance() didn't return an instance of SimpleDateFormat!");
return;
}
SimpleDateFormat sdf = (SimpleDateFormat)test;
String testString1 = "3/10/67";
String testString2 = "3/16/43";
String testString3 = "7/21/43";
// set 2-digit start date to 1/1/1900
Calendar cal = Calendar.getInstance(Locale.US);
cal.set(1900, 0, 1);
sdf.set2DigitStartDate(cal.getTime());
// check to make sure get2DigitStartDate() returns the value we passed to
// set2DigitStartDate()
Date date = sdf.get2DigitStartDate();
cal.setTime(date);
if (cal.get(Calendar.YEAR) != 1900 || cal.get(Calendar.MONTH) != 0 ||
cal.get(Calendar.DATE) != 1)
errln("SimpleDateFormat.get2DigitStartDate() returned " + (cal.get(Calendar.MONTH)
+ 1) + "/" + cal.get(Calendar.DATE) + "/" + cal.get(Calendar.YEAR) +
" instead of 1/1/1900.");
// try parsing "3/10/67" and "3/16/43" with the 2-digit start date set to 1/1/1900
date = sdf.parse(testString1);
cal.setTime(date);
if (cal.get(Calendar.YEAR) != 1967)
errln("Parsing \"3/10/67\" with 2-digit start date set to 1/1/1900 yielded a year of "
+ cal.get(Calendar.YEAR) + " instead of 1967.");
if (cal.get(Calendar.MONTH) != 2 || cal.get(Calendar.DATE) != 10)
errln("Parsing \"3/10/67\" with 2-digit start date set to 1/1/1900 failed: got " +
(cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE) +
" instead of 3/10.");
date = sdf.parse(testString2);
cal.setTime(date);
if (cal.get(Calendar.YEAR) != 1943)
errln("Parsing \"3/16/43\" with 2-digit start date set to 1/1/1900 yielded a year of "
+ cal.get(Calendar.YEAR) + " instead of 1943.");
if (cal.get(Calendar.MONTH) != 2 || cal.get(Calendar.DATE) != 16)
errln("Parsing \"3/16/43\" with 2-digit start date set to 1/1/1900 failed: got " +
(cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE) +
" instead of 3/16.");
// try parsing "3/10/67" and "3/16/43" with the 2-digit start date set to 1/1/2000
cal.set(2000, 0, 1);
sdf.set2DigitStartDate(cal.getTime());
date = sdf.parse(testString1);
cal.setTime(date);
if (cal.get(Calendar.YEAR) != 2067)
errln("Parsing \"3/10/67\" with 2-digit start date set to 1/1/2000 yielded a year of "
+ cal.get(Calendar.YEAR) + " instead of 2067.");
if (cal.get(Calendar.MONTH) != 2 || cal.get(Calendar.DATE) != 10)
errln("Parsing \"3/10/67\" with 2-digit start date set to 1/1/2000 failed: got " +
(cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE) +
" instead of 3/10.");
date = sdf.parse(testString2);
cal.setTime(date);
if (cal.get(Calendar.YEAR) != 2043)
errln("Parsing \"3/16/43\" with 2-digit start date set to 1/1/2000 yielded a year of "
+ cal.get(Calendar.YEAR) + " instead of 1943.");
if (cal.get(Calendar.MONTH) != 2 || cal.get(Calendar.DATE) != 16)
errln("Parsing \"3/16/43\" with 2-digit start date set to 1/1/2000 failed: got " +
(cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE) +
" instead of 3/16.");
// try parsing "3/10/67" and "3/16/43" with the 2-digit start date set to 1/1/1950
cal.set(1950, 0, 1);
sdf.set2DigitStartDate(cal.getTime());
date = sdf.parse(testString1);
cal.setTime(date);
if (cal.get(Calendar.YEAR) != 1967)
errln("Parsing \"3/10/67\" with 2-digit start date set to 1/1/1950 yielded a year of "
+ cal.get(Calendar.YEAR) + " instead of 1967.");
if (cal.get(Calendar.MONTH) != 2 || cal.get(Calendar.DATE) != 10)
errln("Parsing \"3/10/67\" with 2-digit start date set to 1/1/1950 failed: got " +
(cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE) +
" instead of 3/10.");
date = sdf.parse(testString2);
cal.setTime(date);
if (cal.get(Calendar.YEAR) != 2043)
errln("Parsing \"3/16/43\" with 2-digit start date set to 1/1/1950 yielded a year of "
+ cal.get(Calendar.YEAR) + " instead of 1943.");
if (cal.get(Calendar.MONTH) != 2 || cal.get(Calendar.DATE) != 16)
errln("Parsing \"3/16/43\" with 2-digit start date set to 1/1/1950 failed: got " +
(cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE) +
" instead of 3/16.");
// try parsing "3/16/43" and "7/21/43" with the 2-digit start date set to 6/1/1943
cal.set(1943, 5, 1);
sdf.set2DigitStartDate(cal.getTime());
date = sdf.parse(testString2);
cal.setTime(date);
if (cal.get(Calendar.YEAR) != 2043)
errln("Parsing \"3/16/43\" with 2-digit start date set to 6/1/1943 yielded a year of "
+ cal.get(Calendar.YEAR) + " instead of 2043.");
if (cal.get(Calendar.MONTH) != 2 || cal.get(Calendar.DATE) != 16)
errln("Parsing \"3/16/43\" with 2-digit start date set to 6/1/1943 failed: got " +
(cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE) +
" instead of 3/16.");
date = sdf.parse(testString3);
cal.setTime(date);
if (cal.get(Calendar.YEAR) != 1943)
errln("Parsing \"7/21/43\" with 2-digit start date set to 6/1/1943 yielded a year of "
+ cal.get(Calendar.YEAR) + " instead of 1943.");
if (cal.get(Calendar.MONTH) != 6 || cal.get(Calendar.DATE) != 21)
errln("Parsing \"7/21/43\" with 2-digit start date set to 6/1/1943 failed: got " +
(cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE) +
" instead of 7/21.");
// and finally, check one more time to make sure get2DigitStartDate() returns the last
// value we passed to set2DigitStartDate()
date = sdf.get2DigitStartDate();
cal.setTime(date);
if (cal.get(Calendar.YEAR) != 1943 || cal.get(Calendar.MONTH) != 5 ||
cal.get(Calendar.DATE) != 1)
errln("SimpleDateFormat.get2DigitStartDate() returned " + (cal.get(Calendar.MONTH)
+ 1) + "/" + cal.get(Calendar.DATE) + "/" + cal.get(Calendar.YEAR) +
" instead of 6/1/1943.");
}
*/
/**
* ParsePosition.errorIndex tests.
*/
@SuppressWarnings("deprecation")
public void Test4052223()
{
String str = "7/SOS/2001";
Date exp = new Date(101, Calendar.SEPTEMBER, 7);
String pat = "d/MMM/yy";
SimpleDateFormat sdf = new SimpleDateFormat(pat);
ParsePosition pos = new ParsePosition(0);
Date d = sdf.parse(str, pos);
logln(str + " parses with " + pat + " to " + d);
if (d == null && pos.getErrorIndex() == 2) {
logln("Expected null returned, failed at : " + pos.getErrorIndex());
} else {
errln("Failed, parse " + str + " got : " + d + ", index=" + pos.getErrorIndex());
}
}
/**
* Bug4469904 -- th_TH date format doesn't use Thai B.E.
*/
public void TestBuddhistEraBugId4469904() {
String era = "\u0e1e.\u0e28.";
Locale loc = new Locale("th", "TH");
Calendar cal = Calendar.getInstance(Locale.US);
cal.set(2001, 7, 23);
Date date = cal.getTime();
DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, loc);
String output = df.format(date);
int index = output.indexOf(era);
if (index == -1) {
errln("Test4469904: Failed. Buddhist Era abbrev not present.");
}
}
/**
* 4326988: API: SimpleDateFormat throws NullPointerException when parsing with null pattern
*/
@SuppressWarnings("UnusedAssignment")
public void Test4326988() {
String[] wrongPatterns = {
"hh o''clock",
"hh 'o''clock", // unterminated quote
"''''''''''''oclock",
"efgxyz",
};
String[] goodPatterns = {
"hh 'o''clock'",
"'''''''''''''o'",
"'efgxyz'",
":;,.-",
};
// Check NullPointerException
try {
SimpleDateFormat fmt = new SimpleDateFormat(null);
errln("SimpleDateFormat() doesn't throw NPE with null pattern");
} catch (NullPointerException e) {
// Okay
}
try {
Locale loc = null;
SimpleDateFormat fmt = new SimpleDateFormat("yyyy/MM/dd", loc);
errln("SimpleDateFormat() doesn't throw NPE with null locale");
} catch (NullPointerException e) {
// Okay
}
try {
DateFormatSymbols symbols = null;
SimpleDateFormat fmt = new SimpleDateFormat("yyyy/MM/dd", symbols);
errln("SimpleDateFormat() doesn't throw NPE with null DateFormatSymbols");
} catch (NullPointerException e) {
// Okay
}
try {
SimpleDateFormat fmt = new SimpleDateFormat();
fmt.applyPattern(null);
errln("applyPattern() doesn't throw NPE with null pattern");
} catch (NullPointerException e) {
// Okay
}
// Check IllegalParameterException
for (int i = 0; i < wrongPatterns.length; i++) {
try {
SimpleDateFormat fmt = new SimpleDateFormat(wrongPatterns[i]);
errln("SimpleDateFormat(\"" + wrongPatterns[i] + "\")" +
" doesn't throw an IllegalArgumentException");
} catch (IllegalArgumentException e) {
// Okay
}
try {
SimpleDateFormat fmt = new SimpleDateFormat(wrongPatterns[i],
DateFormatSymbols.getInstance());
errln("SimpleDateFormat(\"" + wrongPatterns[i] + "\", DateFormatSymbols) doesn't " +
"throw an IllegalArgumentException");
} catch (IllegalArgumentException e) {
// Okay
}
try {
SimpleDateFormat fmt = new SimpleDateFormat(wrongPatterns[i],
Locale.US);
errln("SimpleDateFormat(\"" + wrongPatterns[i] +
"\", Locale) doesn't throw an IllegalArgumentException");
} catch (IllegalArgumentException e) {
// Okay
}
try {
SimpleDateFormat fmt = new SimpleDateFormat();
fmt.applyPattern(wrongPatterns[i]);
errln("SimpleDateFormat.applyPattern(\"" + wrongPatterns[i] +
"\") doesn't throw an IllegalArgumentException");
} catch (IllegalArgumentException e) {
// Okay
}
}
for (int i = 0; i < goodPatterns.length; i++) {
SimpleDateFormat fmt;
fmt = new SimpleDateFormat(goodPatterns[i]);
fmt = new SimpleDateFormat(goodPatterns[i],
DateFormatSymbols.getInstance());
fmt = new SimpleDateFormat(goodPatterns[i],
Locale.US);
fmt = new SimpleDateFormat();
fmt.applyPattern(goodPatterns[i]);
}
}
/**
* 4486735: RFE: SimpleDateFormat performance improvement
*
* Another round trip test
*/
@SuppressWarnings("deprecation")
public void Test4486735() throws Exception {
TimeZone initialTimeZone = TimeZone.getDefault();
TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
Locale[] locales = Locale.getAvailableLocales();
String[] zones = { "GMT", "America/Los_Angeles", "Europe/London", "Asia/Tokyo" };
// Round to minutes. Some FULL formats don't have seconds.
long time = System.currentTimeMillis()/60000 * 60000;
Date date = new Date(time);
logln("the test date: " + date);
try {
for (int z = 0; z < zones.length; z++) {
TimeZone.setDefault(TimeZone.getTimeZone(zones[z]));
for (int i = 0; i < locales.length; i++) {
Locale loc = locales[i];
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL,
DateFormat.FULL,
loc);
String s = df.format(date);
logln(s);
Date parsedDate = df.parse(s);
long parsedTime = parsedDate.getTime();
if (time != parsedTime) {
// See if the time is in daylight-standard time transition. (JDK-8140571)
// Date-time formats in some locales don't have time zone information.
TimeZone tz = TimeZone.getDefault();
if (tz.inDaylightTime(date) && !tz.inDaylightTime(parsedDate)) {
if (time == parsedTime - tz.getDSTSavings()) {
// OK (in "fall-back")
continue;
}
}
errln("round trip conversion failed: timezone="+zones[z]+
", locale=" + loc +
", expected=" + time + ", got=" + parsedTime);
}
}
}
// Long format test
String pat =
"'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + // 100
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + // 200
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + // 300
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + // 400
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + // 500
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'" +
"\u6642\u9593:\u6642\u9593:\u6642\u9593:\u6642\u9593:\u6642\u9593:" +
"\u6642\u9593:\u6642\u9593:\u6642\u9593:\u6642\u9593:\u6642\u9593:" +
"\u6642\u9593:\u6642\u9593:\u6642\u9593:\u6642\u9593:\u6642\u9593:" +
"\u6642\u9593:\u6642\u9593:\u6642\u9593:\u6642\u9593:\u6642\u9593':'" +
"\u6642\u9593:\u6642\u9593:\u6642\u9593:\u6642\u9593:\u6642\u9593:" +
"yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy" + // 100
"yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy" +
"yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy" + // 200
"yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy" +
"yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy" + // 300
"yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\u5e74";
// Note that >4 y's produces just "2001" until 1.3.1. This
// was fixed in 1.4.
String expected =
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
"\u6642\u9593:\u6642\u9593:\u6642\u9593:\u6642\u9593:\u6642\u9593:" +
"\u6642\u9593:\u6642\u9593:\u6642\u9593:\u6642\u9593:\u6642\u9593:" +
"\u6642\u9593:\u6642\u9593:\u6642\u9593:\u6642\u9593:\u6642\u9593:" +
"\u6642\u9593:\u6642\u9593:\u6642\u9593:\u6642\u9593:\u6642\u9593:" +
"\u6642\u9593:\u6642\u9593:\u6642\u9593:\u6642\u9593:\u6642\u9593:" +
"00000000000000000000000000000000000000000000000000" +
"00000000000000000000000000000000000000000000000000" +
"00000000000000000000000000000000000000000000000000" +
"00000000000000000000000000000000000000000000000000" +
"00000000000000000000000000000000000000000000000000" +
"00000000000000000000000000000000000000000000002001\u5e74";
SimpleDateFormat sdf = new SimpleDateFormat(pat);
String s = sdf.format(new Date(2001-1900, Calendar.JANUARY, 1));
if (!expected.equals(s)) {
errln("wrong format result: expected="+expected+", got="+s);
}
Date longday = sdf.parse(s);
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(longday);
if (cal.get(YEAR) != 2001) {
errln("wrong parse result: expected=2001, got=" + cal.get(YEAR));
}
} catch (Exception e) {
throw e;
} finally {
// Restore the initial time zone
TimeZone.setDefault(initialTimeZone);
}
}
}
|