1

Here is my code

boolean isWithinRange(String d)
{
    boolean withinDate = false;   
    try
    {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = dateFormat.parse(d);
        withinDate = !(date.before(startDate) || date.after(endDate));
    }
    catch (ParseException parseException)
    {
     }
    return withinDate;
}

Inputs

2015-11-26
2015-11-26 - Copy

Both returning true but what i required is "2015-11-26" should be true and "2015-11-26 - Copy" should be false.

Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64

4 Answers4

5

This is because SimpleDateFormat happily parses "2015-11-26" and ignores the " - Copy" part.

The javadoc states :

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.

To detect if the whole string has been used, instead use the parse(String source, ParsePosition pos) method. ParsePosition tells you where parsing stopped. Just compare this with the length of the original date string.

BetaRide
  • 16,207
  • 29
  • 99
  • 177
1

The problem here is we are passing the Date Format 'yyyy-MM-dd'. This will verify the given input upto this format.

For example,

static boolean isWithinRange(String d)
{
    boolean withinDate = false;   
    try
    {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date = dateFormat.parse(d);
        withinDate = !(date.before(startDate) || date.after(endDate));
    }
    catch (ParseException parseException)
    {
        parseException.printStackTrace();
     }
    return withinDate;
}

The above code throw exception because we are passing Date format as 'yyyy-MM-dd hh:mm:ss'. So this will find the hour minutes and seconds

0

just test if size is 10 before your test:

if (d.length!10) return false;
-1
public static void main(String[] args) {
    isWithinRange("2015-12-11 - Copy");
    isWithinRange("2015-12-11");

}

public static boolean isWithinRange(String d) {
    boolean withinDate = false;
    try {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dd = d.indexOf(" ") !=-1 ? d.substring(0, d.indexOf(" ")) : d; // Find valid String 
        if(d.equals(dd)){
        Date date = dateFormat.parse(d);
        withinDate = !(date.before(startDate)) || date.after(endDate)));
        }else{
            withinDate =false;
        }
    } catch (ParseException parseException) {
    }
    return withinDate;
}
Rahul Bhawar
  • 450
  • 7
  • 17
  • That does work only if `d` has a space as the first character which is not part of the date. This would still return `true` for something like `2015-12-11-Copy`. – BetaRide Dec 11 '15 at 06:37
  • User need to take care about first space but he/she confuse about it , trim() the leading and lagging spaces of input string. – Rahul Bhawar Dec 11 '15 at 06:49
  • You will find trim() [http://stackoverflow.com/questions/6652687/strip-leading-and-trailing-spaces-from-java-string] – Rahul Bhawar Dec 11 '15 at 06:52