Formatting milliseconds to hh:mm:ss format

二次信任 提交于 2019-12-24 00:33:16

问题


I am having milliseconds value and want to display the time subtracting 5 minutes from current milliseconds 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();

回答1:


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.




回答2:


Check this solution. It's more elegant

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
String str = sdf.format(new Date(System.currentTimeMillis()));
Toast.makeText(getApplicationContext(), "Alarm Set."+str, Toast.LENGTH_LONG).show()



回答3:


Using the Joda-Time library makes this work much easier.

// Note that milliseconds-since-epoch needs to be a "long" rather than an "int".
long millis = new DateTime().getMillis(); 

// Specify a time zone rather than rely on default.
DateTimeZone timeZone = DateTimeZone.forID( "Asia/Kolkata" ); // Formerly known as Calcutta India.

// Instantiate a DateTime object from number of milliseconds since Unix epoch.
DateTime dateTime = new DateTime( millis, timeZone );

// Go back 5 minutes.
DateTime dateTimeEarlier = dateTime.minusMinutes( 5 );

// Get a formatter to render a string of the time portion.
DateTimeFormatter formatter = ISODateTimeFormat.hourMinuteSecond();

// Use the formatter to create a string.
String output = formatter.print( dateTimeEarlier );

Dump to console…

System.out.println( "millis: " + millis );
System.out.println( "dateTime: " + dateTime );
System.out.println( "dateTimeEarlier: " + dateTimeEarlier );
System.out.println( "output: " + output );

When run…

millis: 1391391422174
dateTime: 2014-02-03T07:07:02.174+05:30
dateTimeEarlier: 2014-02-03T07:02:02.174+05:30
output: 07:02:02


来源:https://stackoverflow.com/questions/21482969/formatting-milliseconds-to-hhmmss-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!