Java – the meaning of “%d:%02d” in `printf`

java

I have written the following code to convert hours into minutes. I got some help from the internet but I'm not 100% sure what "%d:%02d" means?

package time;

public class TimeAssignment {

    public static void main(String[] args) {
        // This program will convert hours into minutes
        int time = 120;
        int hours = time / 60; 
        int minutes = time % 60;
        System.out.printf("%d:%02d", hours, minutes);
    }

}

Best Answer

Even though I'm not 100% sure what ā€œ%d:%02dā€ means

Here you go:

  • %d means an integer
  • : is a :
  • %02d means an integer, left padded with zeros up to 2 digits.

More info at https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html#syntax