java 解决java中时间错误的错误格式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3493378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:04:53  来源:igfitidea点击:

Resolve a Bad format for Time error in java?

javamysqltime

提问by daria

i'm working with java to extract values of a time column in table in mysql.

我正在使用 java 来提取 mysql 表中时间列的值。

the code below show the query i do send.

下面的代码显示了我发送的查询。

String query="select id,time from table where Hour(time)<=32 ";
ResultSet res = stmt.executeQuery(query);
while (res.next()) {
  String id = res.getString(1); 
  Time tc = res.getTime("time");
  System.out.println(tc);
}

the time values can be negative (-56:00:00 meaning that we surpassed a certain delay. the problem is that I get: java.sql.SQLException:java.sql.SQLException: Bad formatfor Time '-05:48:49' in column 2.

时间值可以是负数(-56:00:00 意味着我们超过了一定的延迟。问题是我得到:java.sql.SQLException:java.sql.SQLException: Bad format对于第 2 列中的时间“-05:48:49”。

thanks for your help.

谢谢你的帮助。

采纳答案by BalusC

As answered in your previous questionyou need to store and handle it as seconds which is stored as a signed integer.

正如您在上一个问题中所回答的那样,您需要将其存储为秒,并存储为有符号整数

The time type cannot be negative. You also cannot do math on a varchar/string and massaging it forth and back to a workable format as suggested by Andreas_D would only add unnecessary overhead. A signed integer is really the best datatype you can use for this. Use PreparedStatement#setInt()to store it and use ResultSet#getInt()to obtain it.

时间类型不能为负。您也不能像 Andreas_D 建议的那样对 varchar/string 进行数学运算并将其前后按摩为可行的格式只会增加不必要的开销。有符号整数确实是您可以为此使用的最佳数据类型。使用PreparedStatement#setInt()存储,并使用ResultSet#getInt()获得它。

回答by Andreas Dolk

If the conversion done by the ResultSet implementation does not work for negative time values then you still can read the value as a String and implement your custom method to convert the String to a Date (and vice versa):

如果 ResultSet 实现完成的转换对负时间值不起作用,那么您仍然可以将该值作为字符串读取并实现您的自定义方法以将字符串转换为日期(反之亦然):

  String query="select * from table where Hour(time)<=32 ";
  ResultSet res = stmt.executeQuery(query);
  while (res.next()) {
    String id = res.getString(1); 
    Time tc = convertToDate(res.getString("time"));
    System.out.println(tc);
  }

  // ....

}

private Time convertToDate(String s) {
  // implement magic here

}

回答by Saul Hidalgo

Maybe this answer is so late. But you can solve it just concating a string to the field you want. I mean:

也许这个答案来得太晚了。但是您可以通过将字符串连接到您想要的字段来解决它。我是说:

select id,concat(time,'') from table where Hour(time)<=32

select id,concat(time,'') from table where Hour(time)<=32

Regards Saul Hidalgo.

向索尔·伊达尔戈致敬。

回答by Jose Conde

I think the problem is on the query itself.

我认为问题出在查询本身上。

When you run direcly the query [select * from table where Hour(time)<=32] does it not return you an error? I imagine the error is on the where clause [Hour(time)<=32]

当您直接运行查询 [select * from table where Hour(time)<=32] 时,它不会向您返回错误吗?我想错误是在 where 子句上 [Hour(time)<=32]

The resultset does not have any information about the where clause. It just returns all the columns.

结果集没有关于 where 子句的任何信息。它只返回所有列。

You need to check the columns return to check if you are not returning some strange type.

您需要检查列返回以检查您是否没有返回一些奇怪的类型。