-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
  • 132,755
  • 26
  • 225
  • 256
XorOrNor
  • 8,868
  • 12
  • 48
  • 81
  • 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-
  • 75,269
  • 21
  • 115
  • 152
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
  • 1,100
  • 1
  • 9
  • 23
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,901
  • 21
  • 84
  • 115
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
  • 33,938
  • 5
  • 80
  • 91
-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,702
  • 22
  • 38