JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Checkout for Promo Codes

Program: How to validate date format using regular expression in java?


Description:

The below given example shows how to validate date format using regular expression. This example checks for dd/mm/yyyy date format.


Code:
package com.java2novice.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MyDateFormat {

	private static Pattern dateFrmtPtrn = 
			Pattern.compile("(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)");
	
	public static boolean validateDateFormat(String userName){
		
		Matcher mtch = dateFrmtPtrn.matcher(userName);
		if(mtch.matches()){
			return true;
		}
		return false;
	}
	
	public static void main(String a[]){
		System.out.println("Is '03/04/2012' a valid date format? "
						+validateDateFormat("03/04/2012"));
		System.out.println("Is '12/23/2012' a valid date format? "
						+validateDateFormat("12/23/2012"));
		System.out.println("Is '12/12/12' a valid date format? "
						+validateDateFormat("12/12/12"));
		System.out.println("Is '3/4/2012' a valid date format? "
						+validateDateFormat("3/4/2012"));
	}
}

Output:
Is '03/04/2012' a valid date format? true
Is '12/23/2012' a valid date format? false
Is '12/12/12' a valid date format? false
Is '3/4/2012' a valid date format? true
<< Previous Program | Next Program >>

List Of Regular Expression Sample Programs:

  1. Simple regex pattern matching using string matches().
  2. Simple java regex using Pattern and Matcher classes.
  3. Java regex with case insensitive.
  4. How to validate IP address using regular expression?
  5. How to replace a pattern using regular expression in java?
  6. How to remove multiple spaces with a single space with in a string?
  7. How to validate user name using regular expression?
  8. How to validate email address using regular expression?
  9. How to validate password using regular expression?
  10. How to validate file extensions using regular expression in java?
  11. How to validate date format using regular expression in java?
  12. How to capture or extract a value(s) from text using regular expression in java?
  13. How to split a string using regular expression?
Knowledge Centre
What is System.out in Java
In System.out, out is an instance of PrintStream. It is a static member variable in System class. This is called standard output stream, connected to console.
Famous Quotations
When I do good, I feel good; when I do bad, I feel bad, and that is my religion.
-- Abraham Lincoln

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2020 by Nataraja Gootooru. All Rights Reserved.