提交 4a672f93 authored 作者: Thomas Mueller's avatar Thomas Mueller

Workaround for a java.sql.Date issue: years above 9999 are not formatted…

Workaround for a java.sql.Date issue: years above 9999 are not formatted correctly. When calling ResultSet.getString() on a date value, an alternative formatting algorithm is used.
上级 387316e1
...@@ -71,10 +71,10 @@ public class ValueDate extends Value { ...@@ -71,10 +71,10 @@ public class ValueDate extends Value {
String s = value.toString(); String s = value.toString();
long time = value.getTime(); long time = value.getTime();
// special case: java.sql.Date doesn't format // special case: java.sql.Date doesn't format
// years below year 1 (BC) correctly // years below year 1 (BC) and years above 9999 correctly
if (time < ValueTimestamp.YEAR_ONE) { if (time < ValueTimestamp.YEAR_ONE || time > ValueTimestamp.YEAR_9999) {
int year = DateTimeUtils.getDatePart(value, Calendar.YEAR); int year = DateTimeUtils.getDatePart(value, Calendar.YEAR);
if (year < 1) { if (year < 1 || year > 9999) {
s = year + s.substring(s.indexOf('-')); s = year + s.substring(s.indexOf('-'));
} }
} }
......
...@@ -45,6 +45,14 @@ public class ValueTimestamp extends Value { ...@@ -45,6 +45,14 @@ public class ValueTimestamp extends Value {
*/ */
static final long YEAR_ONE = java.sql.Date.valueOf("0001-01-02").getTime(); static final long YEAR_ONE = java.sql.Date.valueOf("0001-01-02").getTime();
/**
* This is used to find out if the year is possibly larger than 9999.
* Because of time zone issues (the date is time zone specific), it's a few
* days before the last day. That means the value is not exact, but it does
* not need to be.
*/
static final long YEAR_9999 = java.sql.Date.valueOf("9999-12-20").getTime();
private final Timestamp value; private final Timestamp value;
private ValueTimestamp(Timestamp value) { private ValueTimestamp(Timestamp value) {
......
...@@ -729,6 +729,19 @@ public class TestResultSet extends TestBase { ...@@ -729,6 +729,19 @@ public class TestResultSet extends TestBase {
ResultSet rs; ResultSet rs;
Object o; Object o;
rs = stat.executeQuery("call date '99999-12-23'");
rs.next();
assertEquals("99999-12-23", rs.getString(1));
rs = stat.executeQuery("call timestamp '99999-12-23 01:02:03.000'");
rs.next();
assertEquals("99999-12-23 01:02:03.0", rs.getString(1));
rs = stat.executeQuery("call date '-99999-12-23'");
rs.next();
assertEquals("-99999-12-23", rs.getString(1));
rs = stat.executeQuery("call timestamp '-99999-12-23 01:02:03.000'");
rs.next();
assertEquals("-99999-12-23 01:02:03.0", rs.getString(1));
stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY,VALUE DATETIME)"); stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY,VALUE DATETIME)");
stat.execute("INSERT INTO TEST VALUES(1,DATE '2011-11-11')"); stat.execute("INSERT INTO TEST VALUES(1,DATE '2011-11-11')");
stat.execute("INSERT INTO TEST VALUES(2,TIMESTAMP '2002-02-02 02:02:02')"); stat.execute("INSERT INTO TEST VALUES(2,TIMESTAMP '2002-02-02 02:02:02')");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论