[ACCEPTED]-convert String in time to Time object without Date-time

Accepted answer
Score: 29

Use the same SimpleDateFormat that you used to parse it:

String time = "15:30:18";

DateFormat sdf = new SimpleDateFormat("hh:mm:ss");
Date date = sdf.parse(time);

System.out.println("Time: " + sdf.format(date));

Remember, the 5 Date object always represents a combined date/time 4 value. It can't properly represent a date-only 3 or time-only value, so you have to use the 2 correct DateFormat to ensure you only "see" the parts 1 that you want.

Score: 11

This works as well

String t = "00:00:00" 
Time.valueOf(t);

0

Score: 7

Joda-Time | java.time

If you want a time-only value without a 5 date and without a time zone, then you must 4 use either the Joda-Time library or the new java.time 3 package bundled in Java 8 (inspired by Joda-Time).

Both 2 of those frameworks offers a LocalTime class.

In 1 Joda-Time 2.4…

LocalTime localTime = new LocalTime( "15:30:18" );

In java.time…

LocalTime localTime = LocalTime.parse( "15:30:18" );

More Related questions