How we cast a string into time type with mysql in java So String------->java.sql.time
thanks.
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.
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.
In SQL Server, converting a string to date explicitly can be achieved using CONVERT(). CAST() and PARSE() functions.
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);
java.sql.Time.valueOf("String");
You can use this code:
java.sql.Time.parse("String");
But that's deprecated, and replaced by DateFormat Parse.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With