0

I have a Date object (Thu Jul 16 15:54:56 IST 2015).

Date date = new Date();

if(vea.isDateValid("Thu Jul 16 15:04:45 IST 2015","regex")){
    System.out.println("Given Date" + " is a valid date format.");  
} else {  
    System.out.println("Given Date" + " is an invalid date format.");  
}

Please give suggestion for "date" regular Expression without converting to any form.

Jibi Abraham
  • 4,636
  • 2
  • 31
  • 60
Sai
  • 311
  • 1
  • 3
  • 18
  • What is it that you want to do with the date? – nhahtdh Jul 16 '15 at 10:30
  • @anubhava I need to Compare current date and time with Regular Expression.If it matches returns True, else false. – Sai Jul 16 '15 at 10:32
  • Why not compare `Date`s directly? Or event `String#equals`? What are you trying to compare? – MadProgrammer Jul 16 '15 at 10:34
  • @MadProgrammer I don't need any methods to change date format. just i need "regex" for Current date and time from Date(java.util.Date). – Sai Jul 16 '15 at 10:39
  • There A LOT of valid formats for a date (this just 16/07/2015 and Wednesday, 07.16.2015 ). If you don't know the general structure of the String, you can't possibly write a (manageable) single regexp for all the cases – Diego Martinoia Jul 16 '15 at 10:39
  • Once you have the defined format, you can instantiate a SimpleDateFormat case with the approriate String, and try to match. If it fails, it's not valid. If it succeeds, it is. – Diego Martinoia Jul 16 '15 at 10:40
  • @DiegoMartinoia but those are not valid formats for my requirement. – Sai Jul 16 '15 at 10:43
  • Regular Expression will only tell if the "format" is correct, not if the date is valid, which is why people are scratching their heads over your question. Something like [this](http://stackoverflow.com/questions/20231539/java-check-the-date-format-of-current-string-is-according-to-required-format-or/20232680#20232680) will valid a `String` date based on a series of predefined formats, but it will do two things, valid the format AS well as validate the date... – MadProgrammer Jul 16 '15 at 10:43
  • @MadProgrammer my question is not about current date. just need format like that only. – Sai Jul 16 '15 at 10:45
  • So, would something like `Abc Efg 99 99:99:99 HIJ 9999` be a valid value for your format? Cause that's all regular expression will tell, not if the actual value is valid Date value...just saying ;) – MadProgrammer Jul 16 '15 at 10:46
  • @MadProgrammer exactly. – Sai Jul 16 '15 at 10:47
  • ^((31(?! (FEB|APR|JUN|SEP|NOV)))|((30|29)(?! FEB))|(29(?= FEB (((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))|(0?[1-9])|1\d|2[0-8])-(JAN|FEB|MAR|MAY|APR|JUL|JUN|AUG|OCT|SEP|NOV|DEC)-((1[6-9]|[2-9]\d)\d{2})$ – Sai Jul 16 '15 at 10:50
  • I found above format but it's not meet my requirement – Sai Jul 16 '15 at 10:51
  • What is the format you want to parse? Post an example string please. – Diego Martinoia Jul 16 '15 at 10:54

2 Answers2

2

This is the Regex you need :

[A-Z][a-z]{2}\\s[A-Z][a-z]{2}\\s[0-9]{2}\\s[0-9]{2}:[0-9]{2}:[0-9]{2}\\s[A-Z]{3}\\s[0-9]{4}

And in your Java code it would be like this:

Date dt=new Date();
String dateString = dt.toString();

System.out.println(dateString.matches("[A-Z][a-z]{2}\\s[A-Z][a-z]{2}\\s[0-9]{2}\\s[0-9]{2}:[0-9]{2}:[0-9]{2}\\s[A-Z]{3}\\s[0-9]{4}"));

Here's the Working DEMO.

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
0

Something like this should also work, i think it is more convenient to read the format this way:

    SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd hh:mm:ss z YYYY);

    try {
        Date date = sdf.parse(dateToValidate);
    } catch (ParseException e) {
        return false;
    }
    return true;
Viktor Mellgren
  • 4,318
  • 3
  • 42
  • 75