关于 sql:HSQLDB: \\”invalid datetime format\\” for dd/mm/yyyy 但不适用于 yyyy-mm-dd

HSQLDB: "invalid datetime format" for dd/mm/yyyy but not for yyyy-mm-dd

我的表创建如下:

1
2
3
4
5
6
7
CREATE TABLE inventory (
  id INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 100, INCREMENT BY 1) PRIMARY KEY,
  name VARCHAR(30),
  department  VARCHAR(50),
  inventory_type VARCHAR(255),
  expiry_date DATE NOT NULL
);

下面是插入到表中的语句:

1
2
3
INSERT INTO inventory (name, department,inventory_type,expiry_date) VALUES ('om', 'Education','Raw', '01/01/2016');
INSERT INTO inventory (name, department,inventory_type,expiry_date) VALUES ('hari', 'HR','Solid' ,'02/02/2016');
INSERT INTO inventory (name, department,inventory_type,expiry_date) VALUES ('hariom', 'Finance','Other', '03/03/2016');

不幸的是,我得到了

Caused by: java.sql.SQLDataException: data exception: invalid datetime
format at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
~[hsqldb-2.3.2.jar:2.3.2] at
org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
~[hsqldb-2.3.2.jar:2.3.2] at
org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source)
~[hsqldb-2.3.2.jar:2.3.2] at
org.hsqldb.jdbc.JDBCStatement.execute(Unknown Source)
~[hsqldb-2.3.2.jar:2.3.2] at
org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:457)
~[spring-jdbc-4.1.6.RELEASE.jar:4.1.6.RELEASE] ... 60 common frames
omitted

谁能告诉我怎么了?

当我像下面这样更改语句时,它工作正常:

1
2
3
INSERT INTO inventory (name, department, inventory_type, expiry_date) VALUES ('om', 'Education','Raw', '2016-01-01');
INSERT INTO inventory (name, department, inventory_type, expiry_date) VALUES ('hari', 'HR','Solid' ,'2016-02-02');
INSERT INTO inventory (name, department,inventory_type,expiry_date) VALUES ('hariom', 'Finance','Other', '2016-03-03');


Can someone please tell me what is wrong?

答案很简单,如果日期的字符串文字遵循 'yyyy-mm-dd' 格式,HSQLDB 将只接受它们。它愿意提供帮助——在某种程度上——因为它允许我们从"正确的"HSQLDB 日期文字值中省略 DATE 关键字

1
DATE '2016-01-02'

让我们简单地提供

1
'2016-01-02'

但它不会接受 '01/02/2016' (无论如何这是模棱两可的)或我们愚蠢的人类可以表示日期的许多其他方式中的任何一种。