msalam65
asked on
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
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
ASKER
But which method is available that can scan through each array and detemine which one contains the actual date?
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.
If it can be parsed then its a date.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
}
}
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
}
}
ASKER
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
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
see the code I posted above.
ASKER
Thanks Objects. I will look at the code.
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
in your case the format would be something like:
DateFormat df = new SimpleDateFormat("MM/dd/yy ");
DateFormat df = new SimpleDateFormat("MM/dd/yy
And if its a range you're looking for then just split it on "-", and check each part.
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
}
String[] dateAtoms = yourArray[i].split("-");
if (dateAtoms.length == 2) {
// you can now attempt it with *two* DateFormat
}
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
}
}
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
}
}
ASKER
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
CEHJ - What's does your code actually do?
boolean found = false;
final String RE = "((:?\\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
> 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.
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.
>>CEHJ - What's does your code actually do?
Checks to see whether the String array element contains the date range - do you try it?
Checks to see whether the String array element contains the date range - do you try it?
> 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.
differening numbers of characters should not bother the above format, give it a try.
> 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.
not tottaly correct, it just checks for a particular pattern. It does not check if its a valid date or not.
Try it also with each of the formats you mentioned
ASKER
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.
final String RE = "((:?\\d*\\d/){2}\\d{2})-(
Thanks.
>>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
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
>>but can you explain the following code in detail?
How much detail? It looks for the possible date range patterns you mentioned
How much detail? It looks for the possible date range patterns you mentioned
ASKER
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.
final String RE = "((:?\\d*\\d/){2}\\d{2})-(
Please advise.
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.
And you're probably going to need the date values at some point anyway.
>>what will be the pattern different be?
As i mentioned earlier, it will cope with all the patterns you mentioned.
As i mentioned earlier, it will cope with all the patterns you mentioned.
> 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 :)
it has nothing to with dates, it only looks for patterns.
code i posted *does* check for dates :)
ASKER
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..
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..
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
However, since your objective in the question is to *locate* the info, my code should be fine
> 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
then you need to check they are valid dates for insertion into the database
Don't you need to insert the dates into your database?
8-)
Why not?