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

问题描述

我有毫秒的价值,并希望显示 HH时间减去当前毫秒值5分钟:MM:SS 格式

code

 字符串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.toSec​​onds(cal.getTimeInMillis() - 300000) -
                                TimeUnit.MINUTES.toSec​​onds(TimeUnit.MILLISECONDS.toMinutes(cal.getTimeInMillis() - 300000)));Toast.makeText(getApplicationContext(),报警设置。+ STR,Toast.LENGTH_LONG).show()

输出现在

报警设定。 386467:25:00

输出所需

报警设定。 7点25分零零秒

正如你所看到 越来越检索安静的权利,但有一些问题,小时。

P.S

1.I简称。他们说,它的工作原理fine.But不知道为什么没有我的情况。

2.我肯定什么,我希望得到尽可能小时值即 07 正如我已经使用设置的值Calendar.HOUR 和它越来越显示过如果我使用 cal.get(Calendar.HOUR) .CAL是日历类。

替代解决方案

 的SimpleDateFormat自卫队=新的SimpleDateFormat(HH:MM:SS);
 字符串STR1 = sdf.format(新日期(cal.getTimeInMillis() - 300000));
 Toast.makeText(getApplicationContext(),报警集+ STR1,Toast.LENGTH_LONG).show();


解决方案

这是工作的罚款,就是你看到的唯一原因,如此巨大的偏差,是因为它是计算从unix新纪元的总小时数。

当你做了 Calendar.getInstance()它可以让你在当前的时间点。从unix新纪元将其转换为毫秒的总米利斯。

您可以从新纪元检查的总时数:

  //检查自Unix纪元小时
的System.out.println(System.currentTimeMillis的()/ 3600000);

输出:

  386439

您低于code也将产生这样的结果,在时间的分钟和当前点秒追加:

 日历CAL = Calendar.getInstance();字符串str =字符串
        .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.toSec​​onds(cal.getTimeInMillis() - 300000)
                         - TimeUnit.MINUTES.toSec​​onds(TimeUnit.MILLISECONDS
                                .toMinutes(cal.getTimeInMillis() - 300000)));的System.out.println(STR);

输出:

  386439:38:20

注意:您参考示例考虑米利斯的恒定值(3600000),因此它得到一个可读的时候有

更好的解决方案是在为您的要求对方的回答提供的。

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

Code

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

Output now

Alarm Set. 386467:25:00

Output Required

Alarm Set. 07:25:00

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

P.S

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

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.

Alternative Solution

 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();
解决方案

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.

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

Output:

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

Output:

386439:38:20

Note: Your reference example considers a constant value of millis (3600000) hence it gets a readable time there.

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

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

08-07 04:52