-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)?并不是说这个连接对这个问题很重要。 –
对我来说听起来更像是一个忘记了忘记通过这个时代的错误。但这是猜测。 –