This example will center align a string based on a width using java, guava and apache commons.
Straight up Java
This snippet will use core java's String.format to center justify a string. We must find how many characters we need to pad by taking the total width and subtracting the size of the string. The padStart in combination of with String.format will pad the string on the left with spaces. Running the String.format will even out the padding by subtracting the remaining width which will move the string to the middle.
Using guava and a similar technique above we will align the string in the middle of a specified size. Strings.padStart will add spaces to the left of the string or right justify the string. Then calling Strings.padEnd we will add spaces to the right which force the string to be aligned in the center.
Apache commons StringUtils.center hides the complexity that the other two snippets show. By providing the string and the size this method will center the text with spaces.