How do I validate a SSN (Social Security Number) in Java?

This example shows you how to validate a SSN (Social Security Number) in Java.
package net.javaiq.examples.string;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * This class demonstrates on how to validate a SSN (Social Security Number) in Java
 * @author JavaIQ.net
 * Creation Date Dec 10, 2010
 */
public class SsnValidator {

    /** isSSNValid: Validate Social Security number (SSN) using Java reg ex.
     * This method checks if the input string is a valid SSN.
     * @param ssn String. Social Security number to validate
     * @return boolean: true if social security number is valid, false otherwise.
     */
    public static boolean isSsnValid(String ssn) {
        boolean isValid = false;
        /*SSN format xxx-xx-xxxx, xxxxxxxxx, xxx-xxxxxx; xxxxx-xxxx:
             ^\\d{3}: Starts with three numeric digits.
            [- ]?: Followed by an optional "-"
            \\d{2}: Two numeric digits after the optional "-"
            [- ]?: May contain an optional second "-" character.
            \\d{4}: ends with four numeric digits.
            Examples: 879-89-8989; 869878789 etc.
        */

        //Initialize reg ex for SSN. </CODECOMMENTS>
        String expression = "^\\d{3}[- ]?\\d{2}[- ]?\\d{4}$";
        CharSequence inputStr = ssn;
        Pattern pattern = Pattern.compile(expression);
        Matcher matcher = pattern.matcher(inputStr);
        if (matcher.matches()) {
            isValid = true;
        }
        return isValid;
    }

    /**
     * Method to test other methods in the class with sample inputs
     * @param args
     */
    public static void main(String[] args) {
        System.out.println("1. SSN is " + isSsnValid("234-56-7890"));
        System.out.println("2. SSN is " + isSsnValid("234-56-78900"));
        System.out.println("3. SSN is " + isSsnValid("234567890"));
        System.out.println("4. SSN is " + isSsnValid("234567-789"));
    }
}