Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert string into Time object in Java?

I have String value of 08:03:10 pm, and I want to convert it into time. How can I do this in Java?

like image 473
Chamal Avatar asked Jan 03 '11 13:01

Chamal


2 Answers

 String str = "08:03:10 pm";
 DateFormat formatter = new SimpleDateFormat("hh:mm:ss a");
 Date date = formatter.parse(str);
  • IDE One demo

Must See

  • API Doc
like image 111
jmj Avatar answered Sep 17 '22 17:09

jmj


If you omit the period, it is very easy. Just call the java.sql.Time.valueof() method instead of the Time time = new Time("20:03:10"); method.

like image 28
Gursel Koca Avatar answered Sep 20 '22 17:09

Gursel Koca