简体   繁体   English

将字符串解析为时间使得01:00:00

[英]Parsing String to Time makes 01:00:00

When I do the following: 当我执行以下操作时:

String start = request.getParameter("startp");
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");  
long ms=0;
try {
    ms = sdf.parse(start).getTime();
} catch (ParseException e1) {
    e1.printStackTrace();
}
Time ts = new Time(ms);

it is inserted with this value 01:00:00 witch is not the correct one (entered by user). 插入的值是01:00:00 witch不是正确的值(由用户输入)。

I don't understand the error here. 我不明白这里的错误。 Please help. 请帮忙。 Thanks 谢谢

First, it's not casting , it's parsing (I have updated the title and the tags accordingly). 首先,它不是强制转换 ,而是解析 (我已经相应地更新了标题和标签)。 Second, as per the SimpleDateFormat API document two-digit 24h-hours are to be represented by HH , not by hh . 其次,根据SimpleDateFormat API文档,两位数字24小时将由HH而不是hh

To nail the root cause better down, you'll have to update your question to include the actual values of start , ms and ts . 为了确定根本原因,您必须更新问题以包括startmsts实际值。 Print them using System.out.println() or a logger. 使用System.out.println()或记录器打印它们。 If all looks fine, then the problem is in the SQL or the DB table model, you need to ensure that you're setting it using PreparedStatement#setTime() and that the DB table column type is of at least TIME type. 如果一切正常,则问题出在SQL或数据库表模型中,您需要确保使用PreparedStatement#setTime()进行设置,并且数据库表列类型至少为TIME类型。

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

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