Open In App

Java | Date format validation using Regex

Last Updated : 30 Oct, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

We use java.util.regex.Pattern.compile(String regex) method which compiles the given regular expression into a pattern. Here regex is the expression to be compiled.




// Java program to check if given date is
// valid or not.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
  
public class GeeksforGeeks {
  
    // Returns true if d is in format
    // /dd/mm/yyyy
    public static boolean isValidDate(String d)
    {
        String regex = "^(1[0-2]|0[1-9])/(3[01]"
                       + "|[12][0-9]|0[1-9])/[0-9]{4}$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher((CharSequence)d);
        return matcher.matches();
    }
  
    public static void main(String args[])
    {
        System.out.println(isValidDate("10/12/2016"));
        System.out.println(isValidDate("10/02/18"));
    }
}


Output:

true
false

More ways to validate a date :

  1. SimpleDateFormat class. We can use parse method of this class to validate the date.
  2. Writing our own method to check if a date is valid.

Similar Reads

Removing whitespaces using Regex in Java
Given a string, your task is to remove the whitespaces in the string using Java Regex (Regular Expressions). Examples Input : Hello Everyone . Output : HelloEveryone. Input : Geeks for Geeks . Output : GeeksforGeeks.Regular Expressions Regular Expressions or Regex is an API for defining String patterns that can be used for searching, manipulating,
3 min read
Python | Form validation using django
Prerequisites: Django Installation | Introduction to DjangoDjango works on an MVT pattern. So there is a need to create data models (or tables). For every table, a model class is created. Suppose there is a form that takes Username, gender, and text as input from the user, the task is to validate the data and save it.In django this can be done, as
5 min read
<regex> library in C++ STL
Main classes These classes encapsulate a regular expression and the results of matching a regular expression within a target sequence of characters. basic_regex: Regular expression object (class template) sub_match: Identifies the sequence of characters matched by a sub-expression (class template) match_results: Identifies one regular expression ma
2 min read
smatch | Regex (Regular Expressions) in C++
smatch is an instantiation of the match_results class template for matches on string objects. Functions that can be called using smatch: str(), position(), and length() member functions of the match_results object can be called to get the text that was matched, or the starting position and its length of the match relative to the subject string. Cal
3 min read
Convert an image into jpg format using Pillow in Python
Let us see how to convert an image into jpg format in Python. The size of png is larger when compared to jpg format. We also know that some applications might ask for images of smaller sizes. Hence conversion from png(larger ) to jpg(smaller) is needed. For this task we will be using the Image.convert() method of the Pillow module. Algorithm : Impo
2 min read
Python | Working with date and time using Pandas
While working with data, encountering time series data is very usual. Pandas is a very useful tool while working with time series data.  Pandas provide a different set of tools using which we can perform all the necessary tasks on date-time data. Let's try to understand with the examples discussed below. Working with Dates in PandasThe date class i
8 min read
Computer Organization | Problem Solving on Instruction Format
Prerequisite - Basic Computer Instructions, Instruction Formats Problem statement: Consider a computer architecture where instructions are 16 bits long. The first 6 bits of the instruction are reserved for the opcode, and the remaining 10 bits are used for the operands. There are three addressing modes: immediate, direct, and register. For immediat
7 min read
Common Video Format
Whenever a video file is saved it contains two file in it. One is the container and other is codecs. Container defines the structure of the video file and which codecs will be used. Codecs is used to compress and decompress video file. Some of common container format are: Flash Video Format (.flv) This video format is very popular due to the availa
2 min read
Audio Format
Audio format defines the quality and loss of audio data. Based on application different type of audio format are used. Audio formats are broadly divided into three parts: Uncompressed Format Lossy Compressed format Lossless Compressed Format 1. Uncompressed Audio Format: PCM - It stands for Pulse-Code Modulation. It represents raw analog audio sign
3 min read
Ethernet Frame Format
Prerequisite - Introduction to Ethernet Basic frame format which is required for all MAC implementation is defined in IEEE 802.3 standard. Though several optional formats are being used to extend the protocol's basic capability. Ethernet frame starts with Preamble and SFD, both work at the physical layer. Ethernet header contains both the Source an
6 min read
Article Tags :
Practice Tags :