Besides the answer Fernando Lahoz provided (which is pretty specific to your case: decimal formating) you can also use System.out.format
in Java which allows you to specify a format-string while printing to System.out (the format
function is applicable to any PrintStream
though). In your case
System.out.format("%2d %2d", day, month)
should do the trick. The %d
is used for decimal integers and you can then specify any width you want just before the 'd' (2 in your case).
If you want to access the string formed for later use and not (only) print it you can use String.format
. It uses the same format as System.out.format
but returns the String that is formed.
A complete syntax for all formats(string, decimal, floating point, calendar, date/time, ...) can be found here.
If you'd like a quick tuto on number-formatting you can check this link or this link instead.
Good luck!