Java Date determination in an array

Hi,

I am writing a java code where I am selecting a field called description from SQL Server table and parsing the data on a space delimited. For example, the following data will be parsed by using a split method from object class which will now place the split data into an array of String result.

CASUAL GIFT BYRS ONLY 01/01/04-06/30/04 $0-$19 E

So result[0] will have CASUAL, result[1] will have GIFT, etc. I know that result[4] will have 01/01/04-06/30/04, which is a DATE, but I just don't want to use result[4] to get my date. Is there any Java method that will loop through each array element and find which array element contains the Date? Also, the Date can be sometimes in the following fomat. 1/1/04. Please advise.

Thanks,
Mohammed
msalam65Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
>>but I just don't want to use result[4] to get my date

Why not?
0
msalam65Author Commented:
But which method is available that can scan through each array and detemine which one contains the actual date?
0
objectsCommented:
There isn;t one, you'll need to loop thru them yourself, and attempt to parse them with a DateFormat.
If it can be parsed then its a date.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

CEHJCommented:
Try

boolean found = false;
final String RE = "((:?\\d*\\d/){2}\\d{2})-((:?\\d*\\d/){2}\\d{2})";
for(int i = 0;i < yourArray.length && found == false;i++) {
      found = yourArray[i].matches(RE);
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
objectsCommented:
DateFormat df = new SimpleDateFormat(...
df.setLenient(false);
for (int i=0; i<result.length; i++)
{
   try
   {
      Date d = df.parse(result[i]);
      // It's a date
   }
   catch (Exception ex)
   {
       // It's not a date
   }
}
0
msalam65Author Commented:
So how will this loop work?

for(int i = 0; i < result.length; i++) {
   if(result[i].??

I haven't used a DateFormat before, so I am not sure how to check the validation of the array if it really contains a date. Please advise.

Thanks,
Mohammed
0
objectsCommented:
see the code I posted above.
0
msalam65Author Commented:
Thanks Objects. I will look at the code.
0
CEHJCommented:
A DateFormat will not work, since that's not a date that you've got - it's a date range. Use the code i posted to locate this information
0
objectsCommented:
in your case the format would be something like:

DateFormat df = new SimpleDateFormat("MM/dd/yy");
0
objectsCommented:
And if its a range you're looking for then just split it on "-", and check each part.

0
CEHJCommented:
If you want to use DateFormats you'd need to do other things first

String[] dateAtoms = yourArray[i].split("-");
if (dateAtoms.length == 2) {
   // you can now attempt it with *two* DateFormat
}
0
objectsCommented:
DateFormat df = new SimpleDateFormat("MM/dd/yy");
df.setLenient(false);
for (int i=0; i<result.length; i++)
{
   try
   {
      String[] dates = result[i].split("-");
      if (dates.length==2)
      {
          Date from = df.parse(dates[0]);
          Date to = df.parse(dates[1]);
          // It's a date range
      }
      else
      {
          // its not a date range
   }
   catch (Exception ex)
   {
       // It's not a date range
   }
}
0
msalam65Author Commented:
But the issue is my date format can be different, i.e it can be sometime mm/dd/yy-mm/dd/yy or it can be m/dd/yy-mm/dd/yy or m/dd/yy-m/dd/yy. So should I just split it on -?

CEHJ - What's does your code actually do?

boolean found = false;
final String RE = "((:?\\d*\\d/){2}\\d{2})-((:?\\d*\\d/){2}\\d{2})";
for(int i = 0;i < yourArray.length && found == false;i++) {
     found = yourArray[i].matches(RE);
}

Does it just makes the found value from flase to true? Please advise.

Thanks,
Mohammed
0
objectsCommented:
> But the issue is my date format can be different

Then use different formats to test it with.
That way you not only know whether its a date but also what format it was in.
0
CEHJCommented:
>>CEHJ - What's does your code actually do?

Checks to see whether the String array element contains the date range - do you try it?
0
objectsCommented:
>  i.e it can be sometime mm/dd/yy-mm/dd/yy or it can be m/dd/yy-mm/dd/yy or m/dd/yy-m/dd/yy

differening numbers of characters should not bother the above format, give it a try.
0
objectsCommented:
> Checks to see whether the String array element contains the date range

not tottaly correct, it just checks for a particular pattern. It does not check if its a valid date or not.
0
CEHJCommented:
Try it also with each of the formats you mentioned
0
msalam65Author Commented:
I am sorry, but can you explain the following code in detail?

final String RE = "((:?\\d*\\d/){2}\\d{2})-((:?\\d*\\d/){2}\\d{2})";

Thanks.
0
CEHJCommented:
>>not tottaly correct, it just checks for a particular pattern.

Well except for the unlikely event you've got something like

01/32/04-06/30/04

that isn't meant to be a date range, it won't matter
0
CEHJCommented:
>>but can you explain the following code in detail?

How much detail? It looks for the possible date range patterns you mentioned
0
msalam65Author Commented:
So if I want to check for m/dd/yy-mm/dd/yy what will be the pattern different be? I am having hard time what "((:?\\d*\\d/){2}\\d{2})-((:?\\d*\\d/){2}\\d{2})" means?

final String RE = "((:?\\d*\\d/){2}\\d{2})-((:?\\d*\\d/){2}\\d{2})"; (mm/dd/yy-mm/dd/yy)

Please advise.
0
objectsCommented:
But as a perfectly good class exists for parsing dates, why would you use a method that was less accurate.
And you're probably going to need the date values at some point anyway.
0
CEHJCommented:
>>what will be the pattern different be?

As i mentioned earlier, it will cope with all the patterns you mentioned.
0
objectsCommented:
> It looks for the possible date range patterns you mentioned

it has nothing to with dates, it only looks for patterns.

code i posted *does* check for dates :)
0
msalam65Author Commented:
The whole point for this project is as follow.

User enters the following description which gets inserted into SQL SERVER table. I am pulling the following field called description into Java where I need to group by the Date range field to create a report. Date validation doesn't need to given any importance. The description field looks like as follow.

CASUAL GIFT BYRS ONLY 01/01/04-06/30/04 $0-$19 E

CASUAL GIFT BYRS ONLY 7/01/03-12/31/03 $100+ R

CASUAL GIFT BYRS ONLY 7/1/00-12/31/00 $100+ R

So, What I really care is to get the date range field into a field which I can later group by on. So in this 3 example above, I really care of 01/01/04-06/30/04, or 7/01/03-12/31/03 or 7/1/00-12/31/00.

Thanks..
0
CEHJCommented:
As an additional point, you can't have arbitary String date patterns like you mention when it comes to parsing them, as there is no knowing how 01/01/04 should be interpreted. You should try to return the dates as Dates if possible.

However, since your objective in the question is to *locate* the info, my code should be fine
0
objectsCommented:
> I am pulling the following field called description into Java where I need to group by the Date range field

then you need to check they are valid dates for insertion into the database
0
objectsCommented:
Don't you need to insert the dates into your database?
0
CEHJCommented:
8-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.