Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse from SQL Time (String) to java.sql.Time?

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.

The string i recive is like this: 08:00:00

ok, all work's fine here, but i need to have that String in java.sql.Time format.

There is a easy way to do it?

code:

String hour1=retrieveHourFromPHPSqlConnection();
Time a=hour1;

how to transform hour1 into Time?

thanks

like image 902
NullPointerException Avatar asked Dec 13 '10 13:12

NullPointerException


People also ask

How do you convert string to time?

You can use the following code for changing the String value into the time equivalent: String str = "08:03:10 pm"; DateFormat formatter = new SimpleDateFormat("hh:mm:ss a"); Date date = (Date)formatter. parse(str);

What is Java SQL time?

Time is used in the JDBC API, as a wrapper around java. util. Date that handles SQL specific requirements. This class exists to represent SQL TIME, which keeps time without date.


1 Answers

From Javadoc I suggest you to use this :

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.

It's valid at least from Java 1.4.2 up to Java 7.

like image 146
LaGrandMere Avatar answered Oct 28 '22 23:10

LaGrandMere