遇到问题,请先思考。打印一个java.sql.Date对象时,会调用他的toString()方法。java.sql.Date类重写了toString方法。
范例1:它的toString()方法。[code=java] public String toString () {
int year = super.getYear() + 1900;
int month = super.getMonth() + 1;
int day = super.getDate();
char buf[] = "2000-00-00".toCharArray();
buf[0] = Character.forDigit(year/1000,10);
buf[1] = Character.forDigit((year/100)%10,10);
buf[2] = Character.forDigit((year/10)%10,10);
buf[3] = Character.forDigit(year%10,10);
buf[5] = Character.forDigit(month/10,10);
buf[6] = Character.forDigit(month%10,10);
buf[8] = Character.forDigit(day/10,10);
buf[9] = Character.forDigit(day%10,10);
return new String(buf);
}[/code]也就是说,它返回:一个 yyyy-mm-dd 形式的 String 。 |