fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.text.*;
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. String s = "10:15 AM";
  13. SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");
  14. sdf.setLenient(false);
  15. try {
  16. Date dt2 = sdf.parse(s);
  17. System.out.println(dt2);
  18. }
  19. catch (Exception exc) {
  20. System.out.println(s + " IS NOT VALID TIME");
  21. }
  22.  
  23. //Regex
  24. String pat = "[0-1]?[0-2]:[0-5][0-9]\\s*[Aa][Mm]";
  25. if (s.matches(pat)){
  26. System.out.println("Regex matches the string!");
  27. }
  28.  
  29. }
  30. }
Success #stdin #stdout 0.06s 712192KB
stdin
Standard input is empty
stdout
Thu Jan 01 10:15:00 GMT 1970
Regex matches the string!