The format string is still a string, so assuming a width variable System.out.printf("%" + width +"d", x); does the trick.
So for example
var width = 10; var x = 123;System.out.printf("%" + width +"d", x);prints 123 (7 leading spaces + 3 digits = 10), while
var width = 3; var x = 123;System.out.printf("%" + width +"d", x);prints 123