java中的时间毫秒到hh:mm:ss格式 [英] Timemillisecond to hh:mm:ss format in java

查看:75
本文介绍了java中的时间毫秒到hh:mm:ss格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,我正在生成长毫秒的时间戳值.我想将其转换为 HH:mm:ss(600000 到 00:10:00),我希望以 hh:mm:ss 格式显示差异

In my code, I am generating timestamps value that is long millisecond .I want to convert this to HH:mm:ss(600000 to 00:10:00),I want disply difference in hh:mm:ss format

String strstart = "8:30:00";
String strend = "8:40:00";

SimpleDateFormat sdf1 = new SimpleDateFormat("hh:mm:ss");
try {
    Date date1 = sdf1.parse(strstart);

    Date date2 = sdf1.parse(strend);

    long durationInMillis = date2.getTime() - date1.getTime();

    System.out.println("durationInMillis---->" + durationInMillis);

} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

我使用波纹管代码,输出像这样 0:10:0 但我想要像 00:10:00

I use bellow code and the output is 0:10:0 like this but i want the output like 00:10:00

int seconds = (int) (durationInMillis / 1000) % 60 ;
int minutes = (int) ((durationInMillis / (1000*60)) % 60);
int hours   = (int) ((durationInMillis / (1000*60*60)) % 24);

System.out.println(hours+":"+minutes+":"+seconds);

推荐答案

System.out.printf("%02d:%02d:%02d%n", hours, minutes, seconds);

顺便说一下,使用 "HH:mm:ss" 作为 h 用于 12 小时格式 (AM/PM) 和 H> 对于 24 小时制 - 一个有趣的错误,

By the way, use "HH:mm:ss" as h is for the 12 hour format (AM/PM) and H for the 24 hour format - an interesting bug,

这篇关于java中的时间毫秒到hh:mm:ss格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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