Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does an invalid date parses successfully as a real date?

Can someone explain to me how to get the following method to return a value of false for the input shown? It's returning true, which is something I don't expect.

isDateValid("19/06/2012 5:00, 21:00","dd/MM/yyyy HH:mm")

I think this should return false, but apparently Java doesn't think so. The actual date string provided contains these extra characters at the end: ", 21:00".

public static boolean isDateValid(String date, String dateFormat) 
{
        try {
            DateFormat df = new SimpleDateFormat(dateFormat);
            df.setLenient(false);
            Date newDate = df.parse(date);
            System.out.println("Date value after checking for validity: " + newDate);
            return true;
        } catch (ParseException e) {
            return false;
        }
}
like image 722
Joe Avatar asked Aug 22 '15 22:08

Joe


People also ask

Why does JavaScript show invalid date?

The JavaScript exception "invalid date" occurs when a string leading to an invalid date has been provided to Date or Date. parse() .

How do you know if a date is valid?

Given date in format date, month and year in integer. The task is to find whether the date is possible on not. Valid date should range from 1/1/1800 – 31/12/9999 the dates beyond these are invalid. These dates would not only contains range of year but also all the constraints related to a calendar date.

What does it mean to parse a date?

parse() The Date. parse() method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC or NaN if the string is unrecognized or, in some cases, contains illegal date values (e.g. 2015-02-31). Only the ISO 8601 format ( YYYY-MM-DDTHH:mm:ss.

What can I use instead of date parse?

You can use the DATE function instead of the DATEPARSE function if the data you're working with doesn't have the DATEPARSE function or if the field you're trying to convert has a numeric data type. Step 1: The DATE function converts a date type from an integer, string, or date expression.


1 Answers

parse doesn't necessarily use the entire String. This is very clear in the Javadoc, emphasis mine:

parse

public Date parse(String source) throws ParseException

Parses text from the beginning of the given string to produce a date. The method may not use the entire text of the given string. See the parse(String, ParsePosition) method for more information on date parsing.


You can detect if there are extra characters at the end of the string by using parse(String text, ParsePosition pos). If pos is not equal to the end of the String, then there were extra characters remaining at the end.

Here is a working program including test rig that will properly check this the way you intend. In this program, pos.getIndex() will be 0 if it can't parse at all, a number that is too small if there are extra characters at the end, and equal if it works the way you want

public class DateFormatTest {
  public static void main(String[] args) {
    // should be false
    System.out.println(isDateValid("19/06/2012 5:00, 21:00", "dd/MM/yyyy HH:mm"));
    System.out.println(isDateValid("19/06/201", "dd/MM/yyyy HH:mm"));
    
    System.out.println();
    
    // should be true
    System.out.println(isDateValid("19/06/2012 5:00", "dd/MM/yyyy HH:mm"));
  }

  public static boolean isDateValid(String date, String dateFormat) {
    ParsePosition pos = new ParsePosition(0);
    DateFormat df = new SimpleDateFormat(dateFormat);
    df.setLenient(false);
    df.parse(date, pos);

    return pos.getIndex() == date.length();
  }
}
like image 134
durron597 Avatar answered Oct 06 '22 01:10

durron597