Chamal
Chamal

Reputation: 1127

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?

Upvotes: 11

Views: 37950

Answers (3)

hyena
hyena

Reputation: 765

Time does not have a contractor that takes long so use time.set(date.getTime())

Upvotes: 1

Jigar Joshi
Jigar Joshi

Reputation: 240996

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

Must See

Upvotes: 26

Gursel Koca
Gursel Koca

Reputation: 21300

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.

Upvotes: 6

Related Questions