If you use eclipse, you probably format your code by pressing Ctrl+Shift+F or right clicking Source -> Format very often. This function is also provide in JDT, so you can also format your Java code in Java code.
Finding the correct JDT API for doing this job, however, is not straight-forward. In the following code, you will see one of the classes being used is actually an internal class.