DateFormat.java (format): Throw IllegalArgumentException if j' is not a Number or Date instance.

* java/text/DateFormat.java (format): Throw IllegalArgumentException
	if j' is not a Number or Date instance.
	* java/text/SimpleDateFormat.java (tokens): Make it an ArrayList
	instead of Vector.

From-SVN: r71871
This commit is contained in:
Bryce McKinlay 2003-09-28 04:23:29 +00:00 committed by Bryce McKinlay
parent f6b17867e4
commit 6fb708633d
3 changed files with 33 additions and 24 deletions

View file

@ -1,6 +1,13 @@
2003-09-28 Bryce McKinlay <bryce@mckinlay.net.nz> 2003-09-28 Bryce McKinlay <bryce@mckinlay.net.nz>
* java/text/SimpleDateFormat (parse): Revert patch of 2003-09-25. * java/text/DateFormat.java (format): Throw IllegalArgumentException
if `obj' is not a Number or Date instance.
* java/text/SimpleDateFormat.java (tokens): Make it an ArrayList
instead of Vector.
2003-09-28 Bryce McKinlay <bryce@mckinlay.net.nz>
* java/text/SimpleDateFormat.java (parse): Revert patch of 2003-09-25.
Don't call setTimeZone on calendar. Don't call setTimeZone on calendar.
2003-09-27 Michael Koch <konqueror@gmx.de> 2003-09-27 Michael Koch <konqueror@gmx.de>

View file

@ -151,6 +151,8 @@ public abstract class DateFormat extends Format implements Cloneable
{ {
if (obj instanceof Number) if (obj instanceof Number)
obj = new Date(((Number) obj).longValue()); obj = new Date(((Number) obj).longValue());
else if (! (obj instanceof Date))
throw new IllegalArgumentException ("Cannot format given Object as a Date");
return format ((Date) obj, buf, pos); return format ((Date) obj, buf, pos);
} }

View file

@ -39,14 +39,14 @@ exception statement from your version. */
package java.text; package java.text;
import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.Enumeration;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.Locale; import java.util.Locale;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.SimpleTimeZone; import java.util.SimpleTimeZone;
import java.util.Vector;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.IOException; import java.io.IOException;
@ -71,7 +71,7 @@ public class SimpleDateFormat extends DateFormat
} }
} }
private transient Vector tokens; private transient ArrayList tokens;
private DateFormatSymbols formatData; // formatData private DateFormatSymbols formatData; // formatData
private Date defaultCenturyStart; private Date defaultCenturyStart;
private transient int defaultCentury; private transient int defaultCentury;
@ -98,7 +98,7 @@ public class SimpleDateFormat extends DateFormat
set2DigitYearStart(defaultCenturyStart); set2DigitYearStart(defaultCenturyStart);
// Set up items normally taken care of by the constructor. // Set up items normally taken care of by the constructor.
tokens = new Vector(); tokens = new ArrayList();
compileFormat(pattern); compileFormat(pattern);
} }
@ -119,24 +119,24 @@ public class SimpleDateFormat extends DateFormat
current = null; current = null;
if (Character.isLetter(thisChar)) { if (Character.isLetter(thisChar)) {
// Not a valid letter // Not a valid letter
tokens.addElement(new FieldSizePair(-1,0)); tokens.add(new FieldSizePair(-1,0));
} else if (thisChar == '\'') { } else if (thisChar == '\'') {
// Quoted text section; skip to next single quote // Quoted text section; skip to next single quote
pos = pattern.indexOf('\'',i+1); pos = pattern.indexOf('\'',i+1);
if (pos == -1) { if (pos == -1) {
// This ought to be an exception, but spec does not // This ought to be an exception, but spec does not
// let us throw one. // let us throw one.
tokens.addElement(new FieldSizePair(-1,0)); tokens.add(new FieldSizePair(-1,0));
} }
if ((pos+1 < pattern.length()) && (pattern.charAt(pos+1) == '\'')) { if ((pos+1 < pattern.length()) && (pattern.charAt(pos+1) == '\'')) {
tokens.addElement(pattern.substring(i+1,pos+1)); tokens.add(pattern.substring(i+1,pos+1));
} else { } else {
tokens.addElement(pattern.substring(i+1,pos)); tokens.add(pattern.substring(i+1,pos));
} }
i = pos; i = pos;
} else { } else {
// A special character // A special character
tokens.addElement(new Character(thisChar)); tokens.add(new Character(thisChar));
} }
} else { } else {
// A valid field // A valid field
@ -144,22 +144,22 @@ public class SimpleDateFormat extends DateFormat
current.size++; current.size++;
} else { } else {
current = new FieldSizePair(field,1); current = new FieldSizePair(field,1);
tokens.addElement(current); tokens.add(current);
} }
} }
} }
} }
public String toString() public String toString()
{ {
StringBuffer output = new StringBuffer(); StringBuffer output = new StringBuffer();
Enumeration e = tokens.elements(); Iterator i = tokens.iterator();
while (e.hasMoreElements()) { while (i.hasNext()) {
output.append(e.nextElement().toString()); output.append(i.next().toString());
} }
return output.toString(); return output.toString();
} }
/** /**
* Constructs a SimpleDateFormat using the default pattern for * Constructs a SimpleDateFormat using the default pattern for
* the default locale. * the default locale.
@ -175,7 +175,7 @@ public class SimpleDateFormat extends DateFormat
Locale locale = Locale.getDefault(); Locale locale = Locale.getDefault();
calendar = new GregorianCalendar(locale); calendar = new GregorianCalendar(locale);
computeCenturyStart(); computeCenturyStart();
tokens = new Vector(); tokens = new ArrayList();
formatData = new DateFormatSymbols(locale); formatData = new DateFormatSymbols(locale);
pattern = (formatData.dateFormats[DEFAULT] + ' ' pattern = (formatData.dateFormats[DEFAULT] + ' '
+ formatData.timeFormats[DEFAULT]); + formatData.timeFormats[DEFAULT]);
@ -203,7 +203,7 @@ public class SimpleDateFormat extends DateFormat
super(); super();
calendar = new GregorianCalendar(locale); calendar = new GregorianCalendar(locale);
computeCenturyStart(); computeCenturyStart();
tokens = new Vector(); tokens = new ArrayList();
formatData = new DateFormatSymbols(locale); formatData = new DateFormatSymbols(locale);
compileFormat(pattern); compileFormat(pattern);
this.pattern = pattern; this.pattern = pattern;
@ -221,7 +221,7 @@ public class SimpleDateFormat extends DateFormat
super(); super();
calendar = new GregorianCalendar(); calendar = new GregorianCalendar();
computeCenturyStart (); computeCenturyStart ();
tokens = new Vector(); tokens = new ArrayList();
this.formatData = formatData; this.formatData = formatData;
compileFormat(pattern); compileFormat(pattern);
this.pattern = pattern; this.pattern = pattern;
@ -264,7 +264,7 @@ public class SimpleDateFormat extends DateFormat
*/ */
public void applyPattern(String pattern) public void applyPattern(String pattern)
{ {
tokens = new Vector(); tokens = new ArrayList();
compileFormat(pattern); compileFormat(pattern);
this.pattern = pattern; this.pattern = pattern;
} }
@ -418,10 +418,10 @@ public class SimpleDateFormat extends DateFormat
String temp; String temp;
calendar.setTime(date); calendar.setTime(date);
// go through vector, filling in fields where applicable, else toString // go through ArrayList, filling in fields where applicable, else toString
Enumeration e = tokens.elements(); Iterator i = tokens.iterator();
while (e.hasMoreElements()) { while (i.hasNext()) {
Object o = e.nextElement(); Object o = i.next();
if (o instanceof FieldSizePair) { if (o instanceof FieldSizePair) {
FieldSizePair p = (FieldSizePair) o; FieldSizePair p = (FieldSizePair) o;
int beginIndex = buffer.length(); int beginIndex = buffer.length();