• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

validating a range of values

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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) ...
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank's all,
play's about with your examples and it now works fine.
 
Martyn Clark
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thought i had this sussed!
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A 2-digit year will never be greater than 99, right?


It might be, for very large values of 2.


 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
reply
    Bookmark Topic Watch Topic
  • New Topic