5

Im using strings with placeholder in my strings.xml

<string name="date">%1$d.%2$d.%3$d</string>

And set it in the code like:

String.format(context.getResources().getString(R.string.date), day, month, year);

If we take the 5th may for example the result now is:

5.5.2015

How can i add the leading zero for numbers smaler than ten to the string resources?

Not a duplicate of Left padding a String with Zeros since the solution of this question is:

String.format("%010d", Integer.parseInt(mystring));

But the format for the android resource string is:

%1$d

where the 1 indicates the index. Where should i put the 02?

Community
  • 1
  • 1
Mulgard
  • 8,305
  • 26
  • 110
  • 205
  • Use this kind of format "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS" and use String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", dateObject) to get formatted date. – VIjay J Sep 19 '15 at 13:13

1 Answers1

21

You can use this format:

<string name="date">%1$02d.%2$02d.%3$d</string>

For further studying about format, see Formatter's sections: conversion, flags and width.

hata
  • 8,429
  • 5
  • 32
  • 57