Validate a date

From CodeCodex

Implementations[edit]

Java[edit]

import java.text.*;

public class DateUtils {
  public static boolean isValidDateStr(String date) {
    try {
     DateFormat df =
       DateFormat.getDateInstance
         (DateFormat.SHORT); // YYYY-MM-DD
     df.setLenient(false);   // this is important!
     df.parse(date);
    }
    catch (ParseException e) {
     return false;
    }
    catch (IllegalArgumentException e) {
     return false;
    }
    return true;
  }
  
  public static void main(String[] args) {
    System.out.println(" 1900-12-13 valid ? " 
        + DateUtils.isValidDateStr("1900-12-13"));

    // "1990-12/13" throws a ParseException
    System.out.println(" 1900-12/13 valid ? " 
        + DateUtils.isValidDateStr("1900-12/13"));
    // "1990-13-12" throws a IllegalArgumentException
    System.out.println(" 1900-13-12 valid ? " 
        + DateUtils.isValidDateStr("1900-13-12"));
    /*
     * output :
     *  1900-12-13 valid ? true
     *  1900-12/13 valid ? false
     *  1900-13-12 valid ? false
     */
  }
}


Using SimpleDateFormat

package com.rgagnon.howto;

import java.text.*;

public class DateUtils {
  public static boolean isValidDateStr(String date, String format) {
    try {
      SimpleDateFormat sdf = new SimpleDateFormat(format);
      sdf.setLenient(false);
      sdf.parse(date);
    }
    catch (ParseException e) {
      return false;
    }
    catch (IllegalArgumentException e) {
      return false;
    }
    return true;
    }
  
  public static void main(String[] args) {
    System.out.println(" 1900-12-13 valid ? " 
        + DateUtils.isValidDateStr("1900-12-13","yyyy-MM-dd"));

    // "1990-12/13" throws a ParseException
    System.out.println(" 1900-12/13 valid ? " 
        + DateUtils.isValidDateStr("1900-12/13","yyyy-MM-dd"));
    // "1990-13-12" throws a IllegalArgumentException
    System.out.println(" 1900-13-12 valid ? " 
        + DateUtils.isValidDateStr("1900-13-12","yyyy-MM-dd"));
    /*
     * output :
     *  1900-12-13 valid ? true
     *  1900-12/13 valid ? false
     *  1900-13-12 valid ? false
     */
  }
}



Using GregorianCalendar

import java.util.*;

public class jtest {
    public static void main(String args[]) {
        try {
            GregorianCalendar gc = new GregorianCalendar();
            gc.setLenient(false);        // must do this
            gc.set(GregorianCalendar.YEAR, 2003);
            gc.set(GregorianCalendar.MONTH, 42);// invalid month
            gc.set(GregorianCalendar.DATE, 1);

            gc.getTime(); // exception thrown here
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Ruby[edit]

The validation of the string which shows a date.

require 'date'

def valid_date?(str)
  Date.parse(str)
  true
rescue ArgumentError
  false
end

string = ["1900-12-13", "1900-12/13", "1900-13-12",
          "2000/02/29", "May 23, 1968", "2001 NOV. 30"]
string.each do |str|
  puts " #{str} valid ? #{valid_date?(str)}"
end

#  < output >
# 1900-12-13 valid ? true
# 1900-12/13 valid ? false
# 1900-13-12 valid ? false
# 2000/02/29 valid ? true
# May 23, 1968 valid ? true
# 2001 NOV. 30 valid ? true

The validation of the integer (date)

require 'date'

date = [[2010,12,31], [2001,2,29], [2001,13,1]]
date.each do |y,m,d|
  printf " %4d-%02d-%02d valid ? %s\n", y, m, d, Date.valid_date?(y,m,d)
end

#  < output >
# 2010-12-31 valid ? true
# 2001-02-29 valid ? false
# 2001-13-01 valid ? false