Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6641917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:52:03+00:00 2026-05-25T23:52:03+00:00

I want to show a formatted string with minutes, seconds, and only one character

  • 0

I want to show a formatted string with minutes, seconds, and only one character of milliseconds. This is what I put together (mostly from other posts around here):

public static final String getTimeDurationAsString(long milliseconds) {
    int millis  = (int) (milliseconds % 1000);
    int seconds = (int) (milliseconds / 1000) % 60;
    int minutes = (int) ((milliseconds / (1000*60)) % 60);
    int hours   = (int) ((milliseconds / (1000*60*60)) % 24);

    StringBuilder sb = new StringBuilder();
    if (hours > 0) {
        sb.append(String.format("%02d", hours));
        sb.append(":");
    }
    sb.append(String.format("%02d", minutes));
    sb.append(":");
    sb.append(String.format("%02d", seconds));
    sb.append(".");
    sb.append(String.format("%03d", millis).substring(0, 1));

    return sb.toString();
}

So I will hide the hours position if the duration didn’t exceed 59.9 minutes (which will usually be the case). I did a pretty bad substring() for the milliseconds position just to grab the first digit. Is there a better way to do the above? I’d like strings like:

00:14.9
00:05.1
00:05.2
33:20:4
etc

I have to generate this string repeatedly for a game I’m making (the above rendered every frame) so afraid it’s doing a lot of unnecessary work?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:52:04+00:00Added an answer on May 25, 2026 at 11:52 pm

    Without a doubt there are more efficient ways to code this up (there are practically always are).

    However, on my box your function can format ~130,000 timestamps per second. Clearly, this is plenty fast enough for your stated use case.

    That said, if I were coding this up and thought a priori that it’s likely to be on the performance-critical path, I’d avoid String.format():

    public static final String getTimeDurationAsString(long milliseconds) {
        int millis  = (int) (milliseconds % 1000);
        int seconds = (int) (milliseconds / 1000) % 60;
        int minutes = (int) ((milliseconds / (1000*60)) % 60);
        int hours   = (int) ((milliseconds / (1000*60*60)) % 24);
        StringBuilder sb = new StringBuilder();
        if (hours > 0) {
            sb.append((char)('0' + hours / 10))
              .append((char)('0' + hours % 10)).append(":");
        }
        sb.append((char)('0' + minutes / 10))
          .append((char)('0' + minutes % 10)).append(":")
          .append((char)('0' + seconds / 10))
          .append((char)('0' + seconds % 10)).append(".")
          .append((char)('0' + millis / 100));
        return sb.toString();
    }
    

    On my box this can do about 8 million conversions per second. It also creates significantly fewer temporary objects that end up having to be garbage collected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Want to show nicely formatted output regarding bandwidth speed during download I have this
I want to show html formatted string in my winform application. What control should
I want show annotation view (above one of my pin) after load map. For
I want to show my groupview with 2 lines to down direction, but only
I want to send back formatted messages from my domain, For example things like:
i want to show only two decimal places in my edit text, ofc i
I have a string which is formatted like this: $20,$40,$AA,$FF. Basically, hex numbers and
String formatted = String.format(%d:%d, 2, 5); I will output 2:5 But I want: 02:05
I use the .NET Entity Framework. I want to copy properties from one EntityObject
I'm using FPDF (generate PDF from PHP). I want to show all the items

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.