Calendar.MINUTE giving minutes without leading zero

android, java

Solution

As Egor said, an `int` is just an integer. Integers don't have leading zeros. They can only be displayed with leading zeros when you convert them to `String` objects. One way to do that is like this:

String curTime = String.format("%02d:%02d", hrs, mnts);

The format string `%02d` formats an integer with leading zeros (that's the `0` in `%02d`), always taking up 2 digits of width (that's the `2` in `%02d`).

That would produce the `String`

12:09

Problem

Hi all I am using below code to get android phone time, but it is giving me minutes without zero if the minutes are in between 1 to 9. for example:right now I have time on my device 12:09 but its giving me as 12:9 ``` Calendar c = Calendar.getInstance(); int hrs = c.get(Calendar.HOUR); int mnts = c.get(Calendar.MINUTE); String curTime = "" + hrs + ":" + mnts; return curTime; ``` after above code I also try below code its giving same thing as above, minutes without zero before number it the minutes in between 1 to 9 . . ``` final Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(System.currentTimeMillis()); Date date = cal.getTime(); int mHour = date.getHours(); int mMinute = date.getMinutes(); ```

Original source