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 7248633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:09:54+00:00 2026-05-28T22:09:54+00:00

I wrote the following function to convert a time in milliseconds to a string

  • 0

I wrote the following function to convert a time in milliseconds to a string of the format mins:seconds. Being a former C programmer I assumed that “ans” would have to be static in order to work properly, but putting static before String appears to not be allowed.

My question is – will this function work – and if not, what change will make it work.

public String time_to_string(long t) // time in milliseconds
{
    String ans;
    int mins,secs;
    if (t < 0)
    {
        return "-";
    }
    else
    {
        secs = (int)(t/1000);
        mins = secs/60;
        secs = secs - (mins * 60);

        ans = ""+mins+":"+String.format("%02d", secs);

        return ans;
    }
}
  • 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-28T22:09:55+00:00Added an answer on May 28, 2026 at 10:09 pm

    Your code is fine. There’s no problem with returning Strings in this manner.

    In Java, a String is a reference to an immutable object. This, coupled with garbage collection, takes care of much of the potential complexity: you can simply pass a String around without worrying that it would disapper on you, or that someone somewhere would modify it.

    If you don’t mind me making a couple of stylistic suggestions, I’d modify the code like so:

    public String time_to_string(long t) // time in milliseconds
    {
        if (t < 0)
        {
            return "-";
        }
        else
        {
            int secs = (int)(t/1000);
            int mins = secs/60;
            secs = secs - (mins * 60);
            return String.format("%d:%02d", mins, secs);
        }
    }
    

    As you can see, I’ve pushed the variable declarations as far down as I could (this is the preferred style in C++ and Java). I’ve also eliminated ans and have replaced the mix of string concatenation and String.format() with a single call to String.format().

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

Sidebar

Related Questions

I wrote a managed C++ class that has the following function: void EndPointsMappingWrapper::GetLastError(char* strErrorMessage)
I just wrote the following C++ function to programmatically determine how much RAM a
I wrote the following: Object.prototype.length = function(){ var count = -1; for(var i in
I want to call the Sleep function on ASM. So I wrote the following:
I want to write the following procedure / function: procedure ShowSysPopup(aFile: string; x, y:
Is it possible to write a C function that does the following? Allocate a
I wrote following code...but i am getting Error like: Error 1 'LoginDLL.Class1.Login(string, string, string)':
i have the following function i wrote to create an XML file using Xerces
I have tried to write an utility function that can convert my custom data
I wrote a JavaScript function that fires when the user clicks OK in a

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.