如何从 SQL Time (String) 解析为 java.sql.Time?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4429230/
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
How to parse from SQL Time (String) to java.sql.Time?
提问by NullPointerException
i have a database with a Time field. When i get the fields with a php, i receive the Time field as a String, by JSON.
我有一个带有时间字段的数据库。当我使用 php 获取字段时,我通过 JSON 将时间字段作为字符串接收。
The string i recive is like this: 08:00:00
我收到的字符串是这样的: 08:00:00
ok, all work's fine here, but i need to have that String in java.sql.Time format.
好的,这里一切正常,但我需要 java.sql.Time 格式的字符串。
There is a easy way to do it?
有简单的方法吗?
code:
代码:
String hour1=retrieveHourFromPHPSqlConnection();
Time a=hour1;
how to transform hour1 into Time?
如何将小时 1 转换为时间?
thanks
谢谢
回答by LaGrandMere
From JavadocI suggest you to use this :
从Javadoc我建议你使用这个:
java.sql.Time myTime = java.sql.Time.valueOf(hour1);
Takes a String in hh:mm:ss format and gives you a java.sql.Time object.
采用 hh:mm:ss 格式的 String 并为您提供一个 java.sql.Time 对象。
It's valid at least from Java 1.4.2 up to Java 7.
它至少从 Java 1.4.2 到 Java 7 都是有效的。