Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cast a String to sql time

Tags:

java

sql

mysql

How we cast a string into time type with mysql in java So String------->java.sql.time

thanks.

like image 676
kawtousse Avatar asked Apr 28 '10 16:04

kawtousse


People also ask

How do I CAST a timestamp in SQL?

CAST(expression AS [TIMESTAMP | DATETIME | SMALLDATETIME]) represents a date and timestamp with the format YYYY-MM-DD hh:mm:ss. nnn. This value corresponds to the ObjectScript $ZTIMESTAMP special variable. This statement casts a date and time string to the TIMESTAMP data type.

Can you CAST date to datetime in SQL?

We can convert the Date into Datetime in two ways. Using CONVERT() function: Convert means to change the form or value of something. The CONVERT() function in the SQL server is used to convert a value of one type to another type. Convert() function is used to convert a value of any type to another datatype.

Can we convert string to date in SQL?

In SQL Server, converting a string to date explicitly can be achieved using CONVERT(). CAST() and PARSE() functions.


3 Answers

It depends entirely on the format of your String, so I would use a SimpleDateFormat to 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 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);
like image 173
Matt Ball Avatar answered Oct 02 '22 12:10

Matt Ball


java.sql.Time.valueOf("String");
like image 22
Berry Avatar answered Oct 02 '22 11:10

Berry


You can use this code:

java.sql.Time.parse("String");

But that's deprecated, and replaced by DateFormat Parse.

like image 20
Javier Parra Avatar answered Oct 02 '22 12:10

Javier Parra