i got problem to convert String time to Time object because it print together with Date. this is my code.
String time = "15:30:18";
DateFormat sdf = new SimpleDateFormat("hh:mm:ss");
Date date = sdf.parse(time);
System.out.println("Time: " + date);
how to convert and print Time only without Date in java. it will be better if you could give an example.
thank you.
Use the same
SimpleDateFormat
that you used to parse it:Remember, the
Date
object always represents a combined date/time value. It can't properly represent a date-only or time-only value, so you have to use the correctDateFormat
to ensure you only "see" the parts that you want.This works as well
Joda-Time | java.time
If you want a time-only value without a date and without a time zone, then you must use either the Joda-Time library or the new java.time package bundled in Java 8 (inspired by Joda-Time).
Both of those frameworks offers a
LocalTime
class.In Joda-Time 2.4…
In java.time…