将时间字符串转换为没有日期的时间对象 [英] convert String in time to Time object without Date

查看:29
本文介绍了将时间字符串转换为没有日期的时间对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将字符串时间转换为时间对象时遇到问题,因为它与日期一起打印.这是我的代码.

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.

这篇关于将时间字符串转换为没有日期的时间对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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