I am reading from a file containing lines in the format YYYYMMDD. How can I use regular expressions to check the validity of these lines? Thank you. The application is a Java application.
            Asked
            
        
        
            Active
            
        
            Viewed 2,480 times
        
    1
            
            
        - 
                    Regex is not the best option in this case. – Tafari Jan 03 '14 at 07:50
5 Answers
8
            It is impossible with regex. How will you take leap-years into account? You should try to parse the date. If parse throws an exception the date is wrong:
SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd");
f.setLenient(false);  <-- by default SimpleDateFormat allows 20010132
try {
   f.parse(str);
   // good
} catch (ParseExcepton e) {
   // bad
}
 
    
    
        Evgeniy Dorofeev
        
- 133,369
- 30
- 199
- 275
5
            
            
        You should better use SimpleDateFormat with setLenient(false); to validate your date string 
 
    
    
        jmj
        
- 237,923
- 42
- 401
- 438
1
            
            
        like Jigar sugested it is better to use DateFormat. 
public static boolean isValidDate(String date) {
    try {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
      sdf.setLenient(false);
      sdf.parse(date);
    }
    catch (Exception e) {
      return false;
    }
    return true;
  }
helpful link
 
    
    
        stinepike
        
- 54,068
- 14
- 92
- 112
1
            
            
        The answers of @EvgeniyDorofeev, @JigarJoshi and @StinePike are right. But I suggest for bulk data processing a slightly different approach to avoid a logic based on expensive ParseException.
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
sdf.setLenient(false);
for (String line : lines) { // lines from your source
  ParsePosition pos = new ParsePosition(0);
  Date date = sdf.parse(line, pos);
  boolean valid = (date != null);
  // here you can continue with processing the single line
  // can even evaluate the line number and error offset in line
}
Just a remark about regular expressions: How do you want to check the gregorian calendrical rules in such a regular expression? Months have different lengths, there are leap years and so on.
 
    
    
        Meno Hochschild
        
- 42,708
- 7
- 104
- 126
 
     
    