[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Java Date determination in an array

Posted on 2004-11-13
31
Medium Priority
?
201 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:msalam65
  • 13
  • 11
  • 7
31 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12576608
>>but I just don't want to use result[4] to get my date

Why not?
0
 

Author Comment

by:msalam65
ID: 12576617
But which method is available that can scan through each array and detemine which one contains the actual date?
0
 
LVL 92

Expert Comment

by:objects
ID: 12576657
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 12576660
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
 
LVL 92

Expert Comment

by:objects
ID: 12576663
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
 

Author Comment

by:msalam65
ID: 12576679
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
 
LVL 92

Expert Comment

by:objects
ID: 12576682
see the code I posted above.
0
 

Author Comment

by:msalam65
ID: 12576686
Thanks Objects. I will look at the code.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12576698
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
 
LVL 92

Expert Comment

by:objects
ID: 12576701
in your case the format would be something like:

DateFormat df = new SimpleDateFormat("MM/dd/yy");
0
 
LVL 92

Expert Comment

by:objects
ID: 12576704
And if its a range you're looking for then just split it on "-", and check each part.

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12576710
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
 
LVL 92

Expert Comment

by:objects
ID: 12576712
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
 

Author Comment

by:msalam65
ID: 12576728
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
 
LVL 92

Expert Comment

by:objects
ID: 12576734
> 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12576735
>>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
 
LVL 92

Expert Comment

by:objects
ID: 12576736
>  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
 
LVL 92

Expert Comment

by:objects
ID: 12576738
> 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12576739
Try it also with each of the formats you mentioned
0
 

Author Comment

by:msalam65
ID: 12576744
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12576747
>>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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12576750
>>but can you explain the following code in detail?

How much detail? It looks for the possible date range patterns you mentioned
0
 

Author Comment

by:msalam65
ID: 12576754
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
 
LVL 92

Expert Comment

by:objects
ID: 12576759
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12576767
>>what will be the pattern different be?

As i mentioned earlier, it will cope with all the patterns you mentioned.
0
 
LVL 92

Expert Comment

by:objects
ID: 12576768
> 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
 

Author Comment

by:msalam65
ID: 12576780
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12576782
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
 
LVL 92

Expert Comment

by:objects
ID: 12576815
> 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
 
LVL 92

Expert Comment

by:objects
ID: 12600574
Don't you need to insert the dates into your database?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12608492
8-)
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month18 days, 16 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question