java 将字符串转换为 sql 时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2731443/
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
cast a String to sql time
提问by kawtousse
How we cast a string into time type with mysql in java So String------->java.sql.time
我们如何在 java 中使用 mysql 将字符串转换为时间类型 So String------->java.sql.time
thanks.
谢谢。
回答by Matt Ball
It depends entirely on the format of your String, so I would use a SimpleDateFormatto parse the string into a java.util.Date; then you can extract the millisecond time value from that Date and pass it into a java.sql.Time(). Like this:
这完全取决于您的 格式String,所以我会使用 aSimpleDateFormat将字符串解析为java.util.Date; 然后您可以从该日期中提取毫秒时间值并将其传递到java.sql.Time(). 像这样:
String s = /* your date string here */;
SimpleDateFormat sdf = new SimpleDateFormat(/* your date format string here */);
long ms = sdf.parse(s).getTime();
Time t = new Time(ms);
回答by Berry
java.sql.Time.valueOf("String");
回答by Javier Parra
You can use this code:
您可以使用此代码:
java.sql.Time.parse("String");
But that's deprecated, and replaced by DateFormat Parse http://java.sun.com/j2se/1.4.2/docs/api/java/text/DateFormat.html
但这已被弃用,取而代之的是 DateFormat Parse http://java.sun.com/j2se/1.4.2/docs/api/java/text/DateFormat.html

