Win a copy of Penetration Testing Basics this week in the Security forum!

validating a range of values

Martyn Clark
Ranch Hand
Posts: 108
Hi
I have date in the type string as in "0195" where the first two being the month and the last two being year (MMYY) i need to validate this date againt a range say 01/80 through to 12/10 and also test that the month is valid and the year is valid. What i have been doing is parsing the substring of the character as an Integer and assigning the values to variables month and year. that's seems fine but when i test for say the year if i have say 81 as in 1981
the test fails because 81 is > 10, appriciate any help thanks

Joanne Neal
Rancher
Posts: 3742
16
You have to decide what year a two digit date actually represents.
If 10 represents 2010 and 81 represents 1981 then you have to add 2000 to the 10 and 1900 to the 81 and then compare them.

Ernest Friedman-Hill
author and iconoclast
Marshal
Posts: 24212
35
Assuming you know for sure that the year comes from parsing just two characters, then you know it's a number between 0 and 99, inclusive. So you want to say that the year is 1980 or later, or 2010 or earlier; this might look like this:

if (year >= 80 || year <= 10) ...

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
We had to do some of this guessing during the Y2K panic.

Then you could create date objects (assume dd = 01) and compare to your max & min dates. I'd do that to make sure I caught 0113 and 8900 as errors.

Martyn Clark
Ranch Hand
Posts: 108
Thank's all,

Martyn Clark
Ranch Hand
Posts: 108
Still have a problem validating past 2000
what i have is the string value being passed in this format MMYY as in "0785"
now in the method i have:
someMethod(String date)
{
int month=0;
int year=0;
if(date.length()== 4)
{
month = Integer.parseInt(date.substring(0, 2));
year = Integer.parseInt(date.substring(2, 4));
if(year > 99)
{
year = year + 2000;
}
else
{
year = year + 1900;
}
if (month < 1 || month > 12)
{
System.out.println("Invalid Month entered.");
}
if (year < 1981 || year > 2020)
{
System.out.println("Invalid year entered");
}
}
else
{
System.out.println("You must enter in the format MMYY");
}
}

the problem is it works one way but not the other? the string is only allowed to be 4 digits so i only have 2 for the year. I can not seem to get it right. Any help please.

Ernest Friedman-Hill
author and iconoclast
Marshal
Posts: 24212
35
if(year > 99)
{
year = year + 2000;
}
else
{
year = year + 1900;
}

A 2-digit year will never be greater than 99, right? In Stan's example, "cutoff" is some number like 20, so you get years from 1921-2020.

Steve Morrow
Ranch Hand
Posts: 657
A 2-digit year will never be greater than 99, right?

It might be, for very large values of 2.

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
Oops, sorry I didn't describe the cutoff trick better, though I did mention it was a "guess". I can't remember what kind of dates we were dealing with that we thought we could guess with confidence. Your range sounds limited enough that you can, though.

Martyn Clark
Ranch Hand
Posts: 108
Hi all,
ended up with this and it seems to work fine. My problem seemed to be i had the test the wrong way round. I now have:
if(year >=80 && year <= 99)
{
year = year + 1900;
}
else
{
year = year + 2000;
}

Once again thanks for you help...