-3

In the following code timeToDisplay.format seems to have no effect on the timeToDisplay String. tw.setText(timeToDisplay) displays milliseconds instead of expected MM:SS. I've rebuilded the project with no joy.

code:

 TextView tw = (TextView) getView().findViewById(R.id.textView_time);

            long millis = intent.getLongExtra("stamp", 0L);

            String timeToDisplay = String.valueOf(millis);
            timeToDisplay.format("%d min, %d sec",
                    TimeUnit.MILLISECONDS.toMinutes(millis),
                    TimeUnit.MILLISECONDS.toSeconds(millis) -
                            TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));

            tw.setText(timeToDisplay);
Raghunandan
  • 131,557
  • 25
  • 223
  • 252
XorOrNor
  • 8,668
  • 12
  • 44
  • 76
  • 1
    This boils down to **Read The Friendly Manual**. Your title is 100% accurate. – Jonathon Reinhart Jan 14 '14 at 14:25
  • @chrylis provided the proper answer. format is a static method, so `String.format(` should be used. `timeToDisplay.format(` is syntactically correct but is misleading and discouraged. – Glenn Lane Jan 14 '14 at 15:28
  • @Glenn Lane The answer have been accepted already and I'm not going to change it. Your downvoting the question and all of the answers won't change this either... – XorOrNor Jan 14 '14 at 15:44
  • @soulreaver I haven't upvoted/downvoted... I've only deleted my own answer – Glenn Lane Jan 14 '14 at 17:00

5 Answers5

3

Java strings are immutable. You have to assign the result back to a variable:

timeToDisplay = String.format(...);

In your case, you don't need to turn millis into a string at all; the formatter just needs the numbers that you're calculating for it.

chrylis -cautiouslyoptimistic-
  • 72,004
  • 20
  • 117
  • 147
0

calling timeToDisplay.format() will NOT change your actual timeToDisplay reference. However, it will return the new string. Add this to your code:

String formattedTime = timeToDisplay.format("%d min, %d sec",
                TimeUnit.MILLISECONDS.toMinutes(millis),
                TimeUnit.MILLISECONDS.toSeconds(millis) -
                        TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));

Then, use formattedTime in your setText

Alex Vulaj
  • 1,080
  • 1
  • 9
  • 21
0

strings are immutable. try:

timeToDisplay = timeToDisplay.format("%d min, %d sec",
    TimeUnit.MILLISECONDS.toMinutes(millis),
    TimeUnit.MILLISECONDS.toSeconds(millis) -
    TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
Phantômaxx
  • 37,352
  • 21
  • 80
  • 110
0
tw.setText(timeToDisplay.format("%d min, %d sec",
                    TimeUnit.MILLISECONDS.toMinutes(millis),
                    TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))));

Alternatively, you can assign the value of timeToDisplay.format("%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes(millis), TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)))); to a variable and pass that to tw.setText().

hd1
  • 32,598
  • 5
  • 75
  • 87
-1

Strings are immutable, you have to reassign it in this way:

timeToDisplay = timeToDisplay.format("%d min, %d sec",
                TimeUnit.MILLISECONDS.toMinutes(millis),
                TimeUnit.MILLISECONDS.toSeconds(millis) -
                        TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));

That means that the format method cant change timeToDisplay it only returns a new string which is formatted.

kai
  • 6,422
  • 19
  • 38