I’ve taken up some Android programming again and this time in Android Studio, it really does improve the situation in comparison to Eclipse though it’s still far behind Visual Studio + Resharper but since it’s based on IntelliJDEA it does have a lot of the Resharper yumminess embedded and I’m sure we’ll see more of it in the future. But that wasn’t what I intended to rant about this time.

This time it´s about those integers, floats, dates and whatevers. In C# you can almost always find a nicely formatted sensible guide of how to use the String.Format method but in Java I found it more difficult. Sure the documentation page is rather extensive but it still leaves you wanting for some nice examples not having to read the details but rather just get on with it so I thought I’d post the results of my own experiments here as a quick sheet to the results you might want. I apologize a forehand for having this tinted by the Swedish locality since that’s my target for the app I’m writing.

Basically formatting of strings in Java builds on the static String.format(String format, Object …) method. This takes a format string that references instances of the input parameters and formats them using a specific format. In short it looks like this:

In human terms this means:

%[TheItemToFormat][Special formatting rules][Padded final width][.HowToHandleDecimalPoints]TheOutputKindOfType

These arcane formats are a bit tricky to get your head round though but there´s some fairly comprehensible guides on the documentation page, but enough talk, these are examples of what formats give what outputs, if you can´t find what you´re looking for that´s when further readings needed.

[table id=stringformattingjava /]

When it comes to the formatting of dates and times it becomes a bit tricky because the conversion bit takes to parameters, first a prefix t or T depending on whether or not you want upper or lower case output and then the actual bit of information you want from the date time.

Also, this is just a guide using the String.format, there are other ways of formatting especially dates and times like SimpleDateTimeFormat, NumberFormat and other extensions.

So long and fanks for all the formats