• Source
    1. /*
    2. Date : 10 October 2013
    3. Author : Shivam Tiwari [shivamtiwari1011@gmail.com]
    4. Organization : http://mycodedock.blogspot.in/
    5. Description : http://mycodedock.blogspot.in/2013/10/the-task-is-to-create-tool-in-java-that.html
    6. */
    7.  
    8. import java.util.Scanner;
    9. import java.util.regex.Matcher;
    10. import java.util.regex.Pattern;
    11.  
    12. public class Main{
    13. public static void main(String[] args){
    14.  
    15. //declaring required variables
    16. String inputString, findPattern;
    17.  
    18. //creating a new Scanner object
    19. Scanner scannerObj = new Scanner(System.in);
    20.  
    21. /*
    22. (0?[1-9]|[12][0-9]|3[01])
    23. day can be 01, 02....09
    24. OR day can be 1,2....9
    25. OR day can be 10,11....19
    26. OR day can be 20,21....29
    27. OR day can be 30, 31
    28.  
    29. followed by /
    30.  
    31. (0?[1-9]|1[012])
    32. month can be 01,02...09
    33. OR month can be 1,2...9
    34. OR month can be 10, 11, 12
    35.  
    36. followed by /
    37.  
    38. ((19|20)\\d\\d)
    39. year can be 19 followed by digit followed by a digit
    40. OR year can be 20 followed by a digit followed by a digit
    41. */
    42. findPattern = "(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)";
    43.  
    44. System.out.println("Enter date string : ");
    45.  
    46. //get inputString
    47. inputString = scannerObj.nextLine();
    48.  
    49. //create a pattern object
    50. Pattern patternObj = Pattern.compile(findPattern);
    51.  
    52. //create a matcher object
    53. Matcher matcherObj = patternObj.matcher(inputString);
    54.  
    55. if(matcherObj.find()){
    56. System.out.println("This is a valid date string : " + inputString);
    57. }
    58. else{
    59. System.out.println("This is NOT a valid date string : " + inputString);
    60. }
    61. }
    62. }