简体   繁体   English

Oracle SQL中java.sql.Timestamp到日期的转换

[英]java.sql.Timestamp to Date Conversion in Oracle SQL

I have run into this weird Timestamp to Date Conversion issue in Oracle SQL. 我在Oracle SQL中遇到了这个奇怪的时间戳到日期转换问题。

Here is the SQL statement: 这是SQL语句:

String INSERT_SQL = String.format("INSERT INTO AUDIT_TASK (%s, %s, %s, %s) VALUES (AUDIT_TASK_SEQ.nextval,?,?,?)",ID,CLASS_NAME,TASK_STEP_TIMESTAMP,OPERATOR);

java.util.Calendar utcCalendarInstance = Calendar.getInstance(TimeZone .getTimeZone("UTC"));
java.util.Calendar cal = Calendar.getInstance();
final PreparedStatement stmt = con.prepareStatement(INSERT_SQL);
stmt.setString(1, audit.getClassName().getValue());
// Save the timestamp in UTC
stmt.setTimestamp(2,new Timestamp(cal.getTimeInMillis()), utcCalendarInstance);

When I execute this statement, while most of the times the creation_date and task_step_timestamp dates are same, but sometimes I get the task_step_timestamp generated with some bogus dates- like '25-APR-0000' or '00-Jan-0001' etc. 当我执行此语句时,虽然大多数情况下creation_date和task_step_timestamp日期是相同的,但是有时我会生成带有某些假日期的task_step_timestamp,例如'25 -APR-0000'或'00 -Jan-0001'等。

  • ID | ID | Creation_date | 创建日期| Task_step_timestamp Task_step_timestamp
  • 1 |27-APR-2018 17:58:53| 1 | 27-APR-2018 17:58:53 | 25-APR-0000 09:00:45 25-APR-0000 09:00:45
  • 2 |27-APR-2018 18:06:25| 2 | 27-APR-2018 18:06:25 | 00-Jan-0001 09:18:25 00-一月-0001 09:18:25

The data type of task_step_timestamp column in Oracle DB is 'DATE'. Oracle DB中task_step_timestamp列的数据类型为“ DATE”。

Can some one suggest the cause of this inconsistent conversion of timestamp to date? 有人可以建议将时间戳转换为日期不一致的原因吗?

I don't understand why you are using String#format here. 我不明白您为什么在这里使用String#format Just use a regular insert which mentions explicit columns: 只需使用提及显式列的常规插入:

String INSERT_SQL = "INSERT INTO AUDIT_TASK (ID, ERROR_MESSAGE, TASK_STEP_TIMESTAMP, OPERATOR) ";
INSERT_SQL += "VALUES (AUDIT_TASK_SEQ.nextval, ?, ?, ?)";
PreparedStatement stmt = con.prepareStatement(INSERT_SQL);

Then bind your values: 然后绑定您的值:

stmt.setString(1, audit.getErrorMessage() != null ? audit.getErrorMessage().getValue() : null);
stmt.setTimestamp(2, new Timestamp(cal.getTimeInMillis()), utcCalendarInstance);
stmt.setString(3, audit.getClassName().getValue());

Note carefully that the placeholders, in order from left to right, are for the error message, task step timestamp, and operator. 请注意,从左到右的顺序,占位符用于错误消息,任务步骤时间戳和运算符。 Your original code appears to be binding the parameters out of order. 您的原始代码似乎绑定了无序的参数。 By using an insert statement which explicitly mentions the columns, you may avoid this problem. 通过使用显式提及列的插入语句,可以避免此问题。

Edit: 编辑:

It also doesn't make sense to me why you are worrying about time zones for your timestamp. 对于您来说,为什么要担心时间戳的时区,这对我也没有意义。 Just get the numbers of milliseconds since the epoch, and then let the database store that as UTC: 只需获取自纪元以来的毫秒数,然后让数据库将其存储为UTC:

Timestamp timestamp = new Timestamp(System.currentTimeMillis());
stmt.setTimestamp(2, timestamp);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM