字符串到 HH:mm:ss 时间并找出 2 个时间之间的差异
public static void stringToDate(String time, String time2) {
try {
DateFormat formatter = new SimpleDateFormat("HH:mm:ss");
Date t1 = formatter.parse(time);
System.out.println("Users first date: " + t1);
} catch (ParseException e){
System.out.println("Exception :" + e);
}
}
因此,上面,我传入了 2 个字符串参数,它们的格式类似于“17:23:56”,并希望它们都转换为适当的时间对象,然后我可以找到这两个参数之间的差异,可能以毫秒或任何可用的形式如果有人知道那就太好了。
到目前为止我遇到的问题是输出是:“用户第一次日期:Thu Jan 01 17:23:56 GMT 1970”,尽管我认为我指定它只在 HH:mm:ss 中解析它。谁有解决办法,谢谢。
public static void stringToDate(String time, String time2) {
try {
DateFormat formatter = new SimpleDateFormat("HH:mm:ss");
Date t1 = formatter.parse(time);
System.out.println("Users first date: " + t1);
} catch (ParseException e){
System.out.println("Exception :" + e);
}
}
So above, I pass in 2 string parameters which are in the format of something like '17:23:56' and want them both converted into proper time objects that i can then find the difference between the 2, possibly in miliseconds or whatevers available if anyone knows how that'd be great.
Problem i'm having so far is that the output is: "Users first date: Thu Jan 01 17:23:56 GMT 1970", even though I thought I specified it to only parse it in HH:mm:ss. Anyone got the solution, thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在打印 的结果
Date#toString()
(<-- 单击链接!)这确实是给定的格式。如果您想以HH:mm:ss
格式呈现,则必须使用format()
方法对获取的日期
。不用担心这个。只需将其他时间字符串解析为
Date
即可,执行getTime()
两者,最后进行数学计算。You're printing the result of
Date#toString()
( <-- click the link!) which is indeed in the given format. If you want to present it inHH:mm:ss
you have to use theformat()
method on the obtainedDate
.Don't worry about this. Just parse the other time string to
Date
as well, do agetTime()
on both and finally do the math.