Convert HTML special characters to normal text

Apache commons is once again the answer on “how to convert HTML special characters to normal text”.

Just include the commons-lang jar in your project and use only one line of code:
System.out.println(StringEscapeUtils.unescapeHtml3("special chars here"));

Commons Lang

The standard Java libraries fail to provide enough methods for manipulation of its core classes. Apache Commons Lang provides these extra methods.
Lang provides a host of helper utilities for the java.lang API, notably String manipulation methods, basic numerical methods, object reflection, concurrency, creation and serialization and System properties. Additionally it contains basic enhancements to java.util.Date and a series of utilities dedicated to help with building methods, such as hashCode, toString and equals.

Share

Split String in Java

always remember:

public class StringSplit {
  public static void main(String args[]) throws Exception{
    String testString = "Real|How|To";
    // bad
    System.out.println(java.util.Arrays.toString(
        testString.split("|")
    ));
    // output : [, R, e, a, l, |, H, o, w, |, T, o]

    // good
    System.out.println(java.util.Arrays.toString(
      testString.split("\\|")
    ));
    // output : [Real, How, To]
  }
}

The special character needs to be escaped with a “\” but since “\” is also a special character in Java, you need to escape it again with another “\” !

Share

HTTPS URLConnection in gae

I’m playing with the Google App Engine with Java and I am quite excited…

It seems that HttpsURLConnection is not supported in gae, so it is recommended to use the URLConnection.

Using this method in practice seems to cause the following exception in development mode:
javax.net.ssl.SSLHandshakeException: Could not verify SSL certificate for:

but it also seems to work perfectly in production mode if you deploy your application.

Share

Great quote on POJO

Great quote on POJO:

“We wondered why people were so against using regular objects in their systems and concluded that it was because simple objects lacked a fancy name. So we gave them one, and it’s caught on very nicely.”

by Martin Fowler, Rebecca Parsons and Josh MacKenzie in September 2000

Share

Java: Playing with Date, Time, Calendar and SimpleDateFormat

Java: Playing with Date, Time, Calendar and SimpleDateFormat

String time="14:32";
Date t = new Date();
String[] parts = time.split(":");
t.setHours(Integer.valueOf(parts[0]));
t.setMinutes(Integer.valueOf(parts[1]));
Calendar cal = Calendar.getInstance();
cal.setTime(t);
//Go ahead two hours
cal.add(Calendar.HOUR, +2);
//Go back 5 minutes
cal.add(Calendar.MINUTES, -5);
//Display the time
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
return sdf.format(cal.getTimeInMillis());
Share