Parsing and Converting common formats in Java
If you are creating a Web Application in Java you need to know how to correctly and efficiently convert all kinds of string represented data. Below are some of the best Java libraries for parsing and converting various formats in the web world.
UriBuilder is perhaps the most elegant way to create URI’s safely (that includes URL since URI is a superset).
Encoding and Decoding URIs:
Spring UriUtils for various times in which you only need to decode/encode certain parts of the URL. Whatever the case you almost always do not want to use java.net.URLEncoder which is about form encoding and not URL/URI encoding. The name URLEncoder is a misnomer and should probably be called FormEncoding.
Parsing URI Query parameters:
You would think parsing Query parameters would be built into Java but is not. Its a shame because people often try to write their own URI Query parsers… bad idea.
Converting HTML to Text:
Jsoup.parse(html.replace(“ ”, “”)).text();
Gracefully downgrading a UTF-8 String with weird characters to ASCII:
Some times you need to turn a string into ASCII either to be used for friendly URLs or for search engine query synonyms.
Parsing and Formating Dates:
Joda Time http://joda-time.sourceforge.net/apidocs/org/joda/time/DateTime.html#parse(java.lang.String)
While it may seem like a good idea to use SimpleDateFormat it isn’t if you plan on doing this thread safe (it is not so you will have recreate the object or thread-local it).