格式化毫秒为hh:mm:ss格式 [英] Formatting milliseconds to hh:mm:ss format

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

问题描述

我具有毫秒值,并且要以 hh:mm:ss 格式显示从当前毫秒值中减去5分钟的时间。

I am having milliseconds value and want to display the time subtracting 5 minutes from current milliseconds value in hh:mm:ss format.

代码

String str = String.format("%02d:%02d:%02d", 
                                TimeUnit.MILLISECONDS.toHours((cal.getTimeInMillis()-300000)),
                                TimeUnit.MILLISECONDS.toMinutes(cal.getTimeInMillis()-300000) - 
                                TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(cal.getTimeInMillis()-300000)),
                                TimeUnit.MILLISECONDS.toSeconds(cal.getTimeInMillis()-300000) - 
                                TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(cal.getTimeInMillis()-300000)));

Toast.makeText(getApplicationContext(), "Alarm Set."+str, Toast.LENGTH_LONG).show()

立即输出

警报设置。 386467:25:00

需要输出

警报集。 07:25:00

您看到的是分钟 seconds 可以安静地获取,但是 hours 存在一些问题。

As you see minutes and seconds are getting retrieved quiet right but there's some problem with hours.

PS

1。我提到了这篇文章。他们说它很好用,但是不知道为什么不适合我。

1.I referred this post.They say it works fine.But don't know why not in my case.

2.I确定我想获得的小时值,即 07 ,因为我已经使用 Calendar.HOUR 设置了值,并且如果我使用 cal.get(Calendar.HOUR) .cal,它也会显示出来,当然是 Calendar 类的对象。

2.I am sure about what i want to get as hours value i.e 07 as i have set the value using Calendar.HOUR and its getting displayed too if i use cal.get(Calendar.HOUR).cal is of course object of Calendar class.

替代解决方案

 SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
 String str1 = sdf.format(new Date(cal.getTimeInMillis()-300000));
 Toast.makeText(getApplicationContext(), "Alarm Set."+str1, Toast.LENGTH_LONG).show();


推荐答案

工作正常,这是您的唯一原因看到这么大的偏移量是因为它正在计算自UNIX时代以来的总小时数。

It is working fine as is, the only reason you see such a huge offset is because it is calculating the total number of hours since the UNIX epoch.

当您执行 Calendar.getInstance()时它可以为您提供当前时间点。将其转换为毫秒是自UNIX时代以来的总毫秒数。

When you do a Calendar.getInstance() it gets you the current point in time. Converting it to milliseconds are the total millis since the UNIX epoch.

您可以检查自该时代以来的总小时数:

You can check the total number of hours since the epoch:

//Check for the hours since the UNIX Epoch
System.out.println(System.currentTimeMillis() / 3600000);

输出:

386439

下面的代码也将产生此结果加上当前时间的分钟和秒:

You code below would also produce this result appended with the minutes and seconds of the current point in time:

Calendar cal = Calendar.getInstance();

String str = String
        .format("%02d:%02d:%02d",
                TimeUnit.MILLISECONDS.toHours((cal.getTimeInMillis() - 300000)),
                TimeUnit.MILLISECONDS.toMinutes(cal.getTimeInMillis() - 300000)
                        - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS
                                .toHours(cal.getTimeInMillis() - 300000)),
                TimeUnit.MILLISECONDS.toSeconds(cal.getTimeInMillis() - 300000)
                        - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS
                                .toMinutes(cal.getTimeInMillis() - 300000)));

System.out.println(str);

输出:

386439:38:20

注意:您的参考示例考虑了一个常数millis(3600000),因此在该位置具有可读时间。

在另一个答案中提供了更好的解决方案满足您的要求。

The better solution is provided in the other answer which provides for your requirement.

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

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