4

What is the regular expression for Date format dd\mm\yyyy? I am unable to find out regex for this format?

Sandy
  • 6,285
  • 15
  • 65
  • 93
  • 1
    Use the answer from http://stackoverflow.com/questions/3386520/parse-date-from-string-in-this-format-dd-mm-yyyy-to-dd-mm-yyyy and changed the slashes (/) to double backslashes (\\\) – Alex Taylor Nov 27 '11 at 03:25
  • @AlexTaylor Thanks for quick reply, I am looking for regular exp for above date format, can you please tell me that? – Sandy Nov 27 '11 at 03:31
  • 2
    Do you really want a date format with backslashes? – user207421 Jan 22 '15 at 09:34

8 Answers8

9

regex for pattern dd/mm/yyyy

    String regex = "^([0-2][0-9]||3[0-1])/(0[0-9]||1[0-2])/([0-9][0-9])?[0-9][0-9]$";
Vinod Ranga
  • 511
  • 8
  • 15
8

In my opinion, it's best to verify the formatting using a regular expression, but verify the validity with code (Java, in your case). It would be absurdly verbose to try to check things like differing days per month and leap years with a regular expression.

I suggest parsing the date using a regex like ([0-9]{2})\\([0-9]{2})\\([0-9]{4}), then extract each piece (dd, mm, and yyyy, respectively), and try to create a java.util.Date object out of them.

Note the following:

  • Dates are typically written with forward slashes (/) not backslashes (\),
  • In Java strings, to write a backslash character in a regular expression, you will have to actually write \\\\ for two backslash (\\) characters. In java strings to write a backslash we need the escape character (which is again a \).
Cameron
  • 2,574
  • 22
  • 37
Jon Newmuis
  • 25,722
  • 2
  • 45
  • 57
  • +1 Shouldn't it be `([0-9]{2})\\\\([0-9]{2})\\\\([0-9]{4})` ? – blackcompe Nov 27 '11 at 03:43
  • Yes, it should, in a Java string. The regular expression above is kind of the generic regex, but if using a Java string, four slashes need to be used (see note **(b)** at the end of my answer). – Jon Newmuis Nov 27 '11 at 03:45
  • 3
    With this regex you end up accepting invalid date, like "99\99\9999" – HieuHT Jun 28 '17 at 11:50
3
        /**
         * Created with IntelliJ IDEA.
         * User: S34N
         * Date: 2013/07/30
         * Time: 8:21 AM
         * To change this template use File | Settings | File Templates.
         */



        //Import the required classes/packages
        import javax.swing.*;
        import java.text.DateFormat;
        import java.text.SimpleDateFormat;
        import java.util.Date;

        public class dateInputScan {

            public static void main(String args[]) {

                dateInputScan run = new dateInputScan();
                run.dateInputScan();
            }

            public void dateInputScan() {

                //Instantiating variables
                String lvarStrDateOfTransaction = null;
                DateFormat formatter = null;
                Date lvarObjDateOfTransaction = null;

                //Use one of the following date formats.
                lvarStrDateOfTransaction = "29/07/2013";
                //lvarStrDateOfTransaction = "29-07-2013";
                //lvarStrDateOfTransaction = "20130729";
                //lvarStrDateOfTransaction = "2013-07-29";
                //lvarStrDateOfTransaction = "29/07/2013";

                //You can also add your own regex (Regular Expression)
                if (lvarStrDateOfTransaction.matches("([0-9]{2})/([0-9]{2})/([0-9]{4})")) {
                    formatter = new SimpleDateFormat("dd/MM/yyyy");
                } else if (lvarStrDateOfTransaction.matches("([0-9]{2})-([0-9]{2})-([0-9]{4})")) {
                    formatter = new SimpleDateFormat("dd-MM-yyyy");
                } else if (lvarStrDateOfTransaction.matches("([0-9]{4})([0-9]{2})([0-9]{2})")) {
                    formatter = new SimpleDateFormat("yyyyMMdd");
                } else if (lvarStrDateOfTransaction.matches("([0-9]{4})-([0-9]{2})-([0-9]{2})")) {
                    formatter = new SimpleDateFormat("yyyy-MM-dd");
                } else if (lvarStrDateOfTransaction.matches("([0-9]{4})/([0-9]{2})/([0-9]{2})")) {
                    formatter = new SimpleDateFormat("yyyy/MM/dd");
                }

                try {
                    lvarObjDateOfTransaction = formatter.parse(lvarStrDateOfTransaction);
                    JOptionPane.showMessageDialog(null, "Date: " + lvarObjDateOfTransaction);

                } catch (Exception ex) {    //Catch the Exception in case the format is not found.
                    JOptionPane.showMessageDialog(null, ex);
                }
            }
        }
S34N
  • 7,469
  • 6
  • 34
  • 43
1

I tried this ^[0-3]{1}[0-9]{1}/[0-1]{1}[1-2]{1}/[1-9]{1}[0-9]{3}$ and it works

String dateRegEx="^[0-3]{1}[0-9]{1}/[0-1]{1}[1-2]{1}/[1-9]{1}[0-9]{3}$";
System.out.println(Pattern.matches(dateRegEx, "01/01/1990"));
Vandit Upadhyay
  • 315
  • 3
  • 8
1

([0-2][0-9]||3[0-1])\\(0[0-9]||1[0-2])\\((19|20)\d\d)

Above regex will validate 1st 2 character in between range of 0-31 then next group must contain character in between range 0-12 then next group contain year in between range 1900-2099

It is working perfectly fine for me

1

I have done this one for format DDMMYY

([0-2]?[0-2][0-9]|3[0-1])(0?0[1-9]|1[0-2])([0-9][0-9])
Sajeer Babu
  • 1,103
  • 1
  • 9
  • 13
1

If you wanna fixed version use:

  private static final String FIXED_DATE_PATTERN = 
      "(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[012])/((19|20)\\d\\d)";

This force zero in days, and month values, and dont allow inputs like 00/12/2018 or 07/00/2018 in DD/MM/YYYY

leonardo rey
  • 707
  • 7
  • 7
0

for "dd/MM/yyyy" You can use :

(0[1-9]|1[0-9]|2[0-9]|3[0-1]|[1-9])/(0[1-9]|1[0-2]|[1-9])/([0-9]{4})
  • Day (dd): it accept 1 or 01, 9 or 09 (until 31)

  • Month (MM): it accept 1 or 01 (until 12)

  • Year (yyyy): It accept 4 digit

user3032749
  • 113
  • 1
  • 1
  • 6