title: Regex HHmm validation- Java
body: <p>I need to validate a textBox entry which contains time.</p>
  
  <p>Time should be in HH:mm format and in 24-hour format.</p>
  
  <p>For eg: </p>
  
  <pre><code>09:00, 21:00, 00:00, etc.,
  </code></pre>
  
  <p>Invalid entries: </p>
  
  <pre><code>2534, 090, *7&amp;**, etc.,
  </code></pre>
  
  <p>If time entered is in HHmm format, then I need to append a ':'
   to the entry.</p>
  
  <p>For e.g: </p>
  
  <pre><code>If textBox entry= 0930, it should be changed to 09:30
  </code></pre>
  
  <p>This is what I have so far:</p>
  
  <pre><code>String textBoxVal = getTextBoxValue();
  String colonCheck = ":";
  
  if (!textBoxVal.contains(colonCheck)){
  textBoxVal = textBoxVal.substring(0,2) + ":" + textBoxVal.substring(2,4);
  }
  </code></pre>
  
  <p>But as is obvious, this code isn't going to work for all cases.</p>
  
  <p>I'm not very familiar with regex, so any help on how this can be achieved using regex in Java, would be helpful!Thanks!</p>