-509
VS 510
我看到了某種改變或錯誤的數據,與使用JDBC。所以我觀察在Java 8 Update 151上使用H2 Database版本1.4.196。年由負-509改變到JDBC積極的510 H2數據庫
這是一個完整的示例。
注意我們如何檢索日期值三次,第一次作爲一個LocalDate
對象,其次爲文本,第三從鑄造LocalDate
對象提取的int
年數。在文本版中,我們可以看到這一年確實是負面的。神祕的LocalDate
有不同的年份數字,它是積極的,而不是負面的。看起來像一個錯誤。
private void doIt ()
{
System.out.println("BASIL - Running doIt.");
try
{
Class.forName("org.h2.Driver");
} catch (ClassNotFoundException e)
{
e.printStackTrace();
}
try (
Connection conn = DriverManager.getConnection("jdbc:h2:mem:") ; // Unnamed throw-away in-memory database.
)
{
conn.setAutoCommit(true);
String sqlCreate = "CREATE TABLE history (id IDENTITY , when DATE); ";
String sqlInsert = "INSERT INTO history (when) VALUES (?) ; ";
String sqlQueryAll = "SELECT * FROM history ; ";
PreparedStatement psCreate = conn.prepareStatement(sqlCreate);
psCreate.executeUpdate();
PreparedStatement psInsert = conn.prepareStatement(sqlInsert);
psInsert.setObject(1 , LocalDate.of(2017 , 1 , 23));
psInsert.executeUpdate();
psInsert.setObject(1 , LocalDate.of(-509 , 1 , 1));
psInsert.executeUpdate();
PreparedStatement psQueryAll = conn.prepareStatement(sqlQueryAll);
ResultSet rs = psQueryAll.executeQuery();
while (rs.next())
{
long l = rs.getLong(1); // Identity column.
// Retrieve the same date value in three different ways.
LocalDate ld = rs.getObject(2 , LocalDate.class); // Extract a `LocalDate`, and implicitly call its `toString` method that uses standard ISO 8601 formatting.
String s = rs.getString(2); // Extract the date value as text from the database using the database-engine’s own formatting.
int y = ((LocalDate) rs.getObject(2 , LocalDate.class)).getYear(); // Extract the year number as an integer from a `LocalDate` object.
String output = "ROW: " + l+ " | " + ld + " | when as String: " + s+ " | year: " + y ;
System.out.println(output);
}
conn.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
運行時。
ROW:1 | 2017-01-23 |當as字符串:2017-01-23 |年份:2017
ROW:2 | 0510-01-01 |當作爲字符串時:-509-01-01 |年:510
因此,似乎有涉及JDBC的事情發生。請注意,今年如何呈現積極的510而不是消極的509.我不理解這種行爲。
我可以推斷出這是JDBC之內的問題,而不是LocalDate
之內的問題。看到這個example code run live in IdeOne.com顯示一個LocalDate
對象的確帶有並報告了一個負的年份。
LocalDate ld = LocalDate.of(-509 , 1 , 1) ;
System.out.println("ld.toString(): " + ld) ;
System.out.println("ld.getYear(): " + ld.getYear()) ;
注意我們是怎麼做不只有一個LocalDate
打交道時得到從-509轉換爲510,沒有JDBC。
LD:-0509-01-01
ld.getYear():-509
我對H2的項目開了Issue ticket。
有趣的問題。我幾乎不瞭解JDBC,但預感會發生某種形式的下溢。 –
受啓發[爲什麼查詢日期BC更改爲Java中的AD?](https://stackoverflow.com/questions/46835047/why-quering-a-date-bc-is-changed-to-ad-in- JAVA)?並不是說這個連接對這個問題很重要。 –
對我來說聽起來更像是一個忘記了忘記通過這個時代的錯誤。但這是猜測。 –