title: Java: Mask formatting strings
body: <p>I would like to know how to format a string into a specified pattern in Java. For example:</p>
  
  <pre><code>the source string: 01021234
  the desired ouput: 01/02/1234
  </code></pre>
  
  <p>I have followed the MaskFormatter rabit hole, but its proven futile. Could someone clue me in on what function I should be using? Any help is greatly appreciated. </p>
  
  <p>Please see Sample code:</p>
  
  <pre><code>private static Date prepData(String date, String time) {
      try {
          if (date != null || !date.equals("")) {
              if (date.contains("/")) {
                  return new Date(date + " " + time.substring(0, time.indexOf('.')));
              }else{
                  MaskFormatter mk = new MaskFormatter("##/##/####");
                  mk.setValidCharacters("1234567890");
                  System.out.println(mk.valueToString(date));
              }
          } else {
              return null;
          }
      } catch (Exception ex) {
      }
      return null;
  }
  </code></pre>