Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
126 views
Welcome To Ask or Share your Answers For Others

1 Answer

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 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 correct DateFormat to ensure you only "see" the parts that you want.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...