将String转换为Time对象,而不使用Date [英] convert String in time to Time object without Date

查看:99
本文介绍了将String转换为Time对象,而不使用Date的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题将String时间转换为Time对象,因为它与Date一起打印。这是我的代码。

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);

如何转换和打印在java中没有日期的时间。

how to convert and print Time only without Date in java. it will be better if you could give an example.

谢谢。

推荐答案

使用您用来解析的相同的 SimpleDateFormat

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));

记住, Date 对象始终代表组合日期/时间值。它不能正确表示仅日期或仅时间的值,因此您必须使用正确的 DateFormat 来确保您只看到所需的部分。

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.

这篇关于将String转换为Time对象,而不使用Date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆