title: java.lang.StringIndexOutOfBoundsException: String index out of range: -4
body: <p>I have this simple Java code which gives the following error:
  java.lang.StringIndexOutOfBoundsException: String index out of range: -4</p>
  
  <p>the code is:</p>
  
  <pre><code>String date = "14000101";
  String repayDate = date.substring(0, 4)+"-"+date.substring(5, 2)+"-"+date.substring(6, 2);
  </code></pre>
  
  <p>the length of the string is 8 and yet it gives an error starting form the second part. </p>
  
  <p>Any ideas?</p>
  
  <p>Thanks!</p>