• Post Reply Bookmark Topic Watch Topic
  • New Topic

Date recognition from String  RSS feed

 
Rob McCarthy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have an application that reads in information files in text format. Each line is made up of some character delimited fields and one of these fields is a date field. This information files come from can come from different sources and can have different formats, ie the columns in different orders and the date in different formats.
The file will always be the in same format from a particular source so I wrote a utility into my program that you can run which lets to create a defintion file which maps the column data in the file to the appropriate field in the application, so when you import you just choose the right definition and it reads everything correctly.
The problem is with the date. It can possibly come in many different formats. Is there any way to determine the date from a string if you are unsure of the format when it is accessed? So if "2004-04-15" was brought in as a string the applicaiton would parse that as year 2004, march 15. I know you can use SimpleDateFormat to parse strings into dates but it requires you to know the format in advance. Anyone have any recommendations on how I should handle this?
Any help is appreciated.
 
Tom Blough
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SimpleDateFormat.parse( String) throws a ParseException (be sure to setLenient(False)), when unable to parse the date string. You could easily set up multiple SimpleDateFormaters, one for each expected date format, and try them in turn until one does not throw an exception.
 
Max Habibi
town drunk
( and author)
Sheriff
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob,
If you're using jdk 1.4, and if you describe your format in a little more detail, I might be able to help with a regex solution.
M
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beware that there may be some situations where you can't determine the date without knowing the format. E.g. if the date is 07-10-04 - is that July 10, 2004 (US notation, MM-dd-yy), or Sept 7, 2004 (dd-MM-yy for the rest of the world)? Or perhaps it's April 10, 2007 (yy-MM-dd) which is best for sorting. Now if you know that the data is always in American format, or that it's never in American format, then you can probably solve this problem. But if some of your data sources are American and some aren't, then for some dates there's simply no way to know whether it's America or not simply by looking at it. In this case you will probably need to look at other parts of your data to see if it provides clues. E.g. does one of the other fields contain a country name? Hopefully there's enough info present to solve the problem (if indeed you have this problem). Just something to think about...
 
Rob McCarthy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, thanks for the replys. With regards to the date they will mainly be in American format but there is also a possibility for international use so I was hoping to get a solution that might work around that problem.
I can't tell all the formats that might show up so it makes the problem/solution a little harder. When the user creates the defintion file for a particular input file format I was thinking of prompting them to select the general format of the date when they try to map the date field. If it displayed the date to them and then asked them to select the general order from a drop down box (ie year-month-day, month-day-year, year-month-day-time....). If i know the order of the date and have the actual date then I may be able to just parse it myself.
One last thing about the format. I only store the month,day and year in the database, so it doesn't matter about any time or timezone information also in the date.
I would just need then to figure out the best way of parsing that, anyone have any recommendations? This solution would then be usable in all locales.
Thanks,
Rob
[ April 22, 2004: Message edited by: Rob McCarthy ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!