errln("A time instance shouldn't equal a default date format");
}
Date d;
{
ChineseDateFormat fmt = new ChineseDateFormat("yymm", Locale.US);
try {
fmt.parse("2"); // fewer symbols than required 2
errln("whoops");
}
catch (ParseException e) {
logln("ok");
}
try {
fmt.parse("2255"); // should succeed with obeycount
logln("ok");
}
catch (ParseException e) {
errln("whoops");
}
try {
fmt.parse("ni hao"); // not a number, should fail
errln("whoops ni hao");
}
catch (ParseException e) {
logln("ok ni hao");
}
}
{
Calendar xcal = new GregorianCalendar();
xcal.set(Calendar.HOUR_OF_DAY, 0);
DateFormat fmt = new SimpleDateFormat("k");
StringBuffer xbuf = new StringBuffer();
FieldPosition fpos = new FieldPosition(Calendar.HOUR_OF_DAY);
fmt.format(xcal, xbuf, fpos);
try {
fmt.parse(xbuf.toString());
logln("ok");
xbuf.setLength(0);
xcal.set(Calendar.HOUR_OF_DAY, 25);
fmt.format(xcal, xbuf, fpos);
Date d2 = fmt.parse(xbuf.toString());
logln("ok again - d2=" + d2);
}
catch (ParseException e) {
errln("whoops");
}
}
{
// cover gmt+hh:mm
DateFormat fmt = new SimpleDateFormat("MM/dd/yy z");
try {
d = fmt.parse("07/10/53 GMT+10:00");
logln("ok : d = " + d);
}
catch (ParseException e) {
errln("Parse of 07/10/53 GMT+10:00 for pattern MM/dd/yy z");
}
// cover invalid separator after GMT
{
ParsePosition pp = new ParsePosition(0);
String text = "07/10/53 GMT=10:00";
d = fmt.parse(text, pp);
if(pp.getIndex()!=12){
errln("Parse of 07/10/53 GMT=10:00 for pattern MM/dd/yy z");
}
logln("Parsing of the text stopped at pos: " + pp.getIndex() + " as expected and length is "+text.length());
}
// cover bad text after GMT+.
try {
fmt.parse("07/10/53 GMT+blecch");
logln("ok GMT+blecch");
}
catch (ParseException e) {
errln("whoops GMT+blecch");
}
// cover bad text after GMT+hh:.
try {
fmt.parse("07/10/53 GMT+07:blecch");
logln("ok GMT+xx:blecch");
}
catch (ParseException e) {
errln("whoops GMT+xx:blecch");
}
// cover no ':' GMT+#, # < 24 (hh)
try {
d = fmt.parse("07/10/53 GMT+07");
logln("ok GMT+07");
}
catch (ParseException e) {
errln("Parse of 07/10/53 GMT+07 for pattern MM/dd/yy z");
}
// cover no ':' GMT+#, # > 24 (hhmm)
try {
d = fmt.parse("07/10/53 GMT+0730");
logln("ok");
}
catch (ParseException e) {
errln("Parse of 07/10/53 GMT+0730 for pattern MM/dd/yy z");
}
// cover GMT+#, # with second field
try {
d = fmt.parse("07/10/53 GMT+07:30:15");
logln("ok GMT+07:30:15");
}
catch (ParseException e) {
errln("Parse of 07/10/53 GMT+07:30:15 for pattern MM/dd/yy z");
}
// cover no ':' GMT+#, # with second field, no leading zero
try {
d = fmt.parse("07/10/53 GMT+73015");
logln("ok GMT+73015");
}
catch (ParseException e) {
errln("Parse of 07/10/53 GMT+73015 for pattern MM/dd/yy z");
}
// cover no ':' GMT+#, # with 1 digit second field
try {
d = fmt.parse("07/10/53 GMT+07300");
logln("ok GMT+07300");
}
catch (ParseException e) {
errln("Parse of 07/10/53 GMT+07300 for pattern MM/dd/yy z");
}
// cover raw digits with no leading sign (bad RFC822)
try {
d = fmt.parse("07/10/53 07");
errln("Parse of 07/10/53 07 for pattern MM/dd/yy z passed!");
}
catch (ParseException e) {
logln("ok");
}
// cover raw digits (RFC822)
try {
d = fmt.parse("07/10/53 +07");
logln("ok");
}
catch (ParseException e) {
errln("Parse of 07/10/53 +07 for pattern MM/dd/yy z failed");
}
// cover raw digits (RFC822)
try {
d = fmt.parse("07/10/53 -0730");
logln("ok");
}
catch (ParseException e) {
errln("Parse of 07/10/53 -00730 for pattern MM/dd/yy z failed");
}
// cover raw digits (RFC822) in DST
try {
fmt.setTimeZone(TimeZone.getTimeZone("PDT"));
d = fmt.parse("07/10/53 -0730");
logln("ok");
}
catch (ParseException e) {
errln("Parse of 07/10/53 -0730 for pattern MM/dd/yy z failed");
}
}
// TODO: revisit toLocalizedPattern
if (false) {
SimpleDateFormat fmt = new SimpleDateFormat("aabbcc");
try {
String pat = fmt.toLocalizedPattern();
errln("whoops, shouldn't have been able to localize aabbcc");
}
catch (IllegalArgumentException e) {
logln("aabbcc localize ok");
}
}
{
SimpleDateFormat fmt = new SimpleDateFormat("'aabbcc");
try {
fmt.toLocalizedPattern();
errln("whoops, localize unclosed quote");
}
catch (IllegalArgumentException e) {
logln("localize unclosed quote ok");
}
}
{
SimpleDateFormat fmt = new SimpleDateFormat("MM/dd/yy z");
String text = "08/15/58 DBDY"; // bogus time zone
try {
fmt.parse(text);
errln("recognized bogus time zone DBDY");
}
catch (ParseException e) {
logln("time zone ex ok");
}
}
{
// force fallback to default timezone when fmt timezone
// is not named
SimpleDateFormat fmt = new SimpleDateFormat("MM/dd/yy z");
// force fallback to default time zone, still fails
fmt.setTimeZone(TimeZone.getTimeZone("GMT+0147")); // not in equivalency group
String text = "08/15/58 DBDY";
try {
fmt.parse(text);
errln("Parse of 07/10/53 DBDY for pattern MM/dd/yy z passed");
}
catch (ParseException e) {
logln("time zone ex2 ok");
}
// force success on fallback
text = "08/15/58 " + TimeZone.getDefault().getID();
try {
fmt.parse(text);
logln("found default tz");
}
catch (ParseException e) {
errln("whoops, got parse exception");
}
}
{
// force fallback to symbols list of timezones when neither
// fmt and default timezone is named
SimpleDateFormat fmt = new SimpleDateFormat("MM/dd/yy z");
TimeZone oldtz = TimeZone.getDefault();
TimeZone newtz = TimeZone.getTimeZone("GMT+0137"); // nonstandard tz
fmt.setTimeZone(newtz);
TimeZone.setDefault(newtz); // todo: fix security issue
// fallback to symbol list, but fail
String text = "08/15/58 DBDY"; // try to parse the bogus time zone
try {
fmt.parse(text);
errln("Parse of 07/10/53 DBDY for pattern MM/dd/yy z passed");
}
catch (ParseException e) {
logln("time zone ex3 ok");
}
catch (Exception e) {
// hmmm... this shouldn't happen. don't want to exit this
// fn with timezone improperly set, so just in case
TimeZone.setDefault(oldtz);
throw new IllegalStateException(e.getMessage());
}
// create DFS that recognizes our bogus time zone, sortof
DateFormatSymbols xsym = new DateFormatSymbols();
String[][] tzids = xsym.getZoneStrings();
if (tzids.length > 0) { // let's hope!
tzids[0][1] = "DBDY"; // change a local name
logln("replaced '" + tzids[0][0] + "' with DBDY");
xsym.setZoneStrings(tzids);
fmt.setDateFormatSymbols(xsym);
try {
fmt.parse(text);
logln("we parsed DBDY (as GMT, but still...)");
}
catch (ParseException e) {
errln("hey, still didn't recognize DBDY");
}