1

My application reads a csv file provided by the user. This csv file contains a field for date and / or time, formatted in any way the user chose it. Like: 1999/10/28 or 2000-01-01,3PM-02-59

I want to offer the user the possibility to indicate the formatting of the date and time in this field. So, I'd have an input window saying: "Specify the formatting of your date/time field" "Example: for 1999/10/28 format, input yyyy/mm/dd" "Other example: for 2000-01-01,3PM-02-59,input yyyy-dd-mm,hh-mm-ss"

I would retrieve their input as a string, then do some complicated and annoying operation to derive a regular expression from this string, which I would then apply in the csv parser to identify years, months, days, hours, etc. in the "date and time" field.

My question before embarking in this: is there a library which already does this kind of thing? Am I reinventing the wheel? Is there a much more obvious approach I'm missing?

Thanks a lot!

PS: yes, it is all about users providing their own strange date and time formats. No need to suggest that it would be easier with a a uniform format! :-)

seinecle
  • 10,118
  • 14
  • 61
  • 120

2 Answers2

1

in Joda Time you can use multiple parser, you can check this SO answer Using Joda Date & Time API to parse multiple formats

Community
  • 1
  • 1
Rohi
  • 385
  • 6
  • 22
  • Thanks! That's a first step. I would need to dynamically create a new parser based on the input of the user? Seems feasible... – seinecle Feb 23 '12 at 11:13
  • Yes, since the user is prompted to choose the format he prefers, you can pass that as input parameter to the method in charge of the date parsing – Rohi Feb 23 '12 at 12:54
0

you might want to take a look at

http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

Fredszaq
  • 3,091
  • 1
  • 18
  • 20
  • thanks but this is the opposite of what I want. SimpleDateFormat takes a formatted date and converts it in a variety of forms. I need to take a date formatted in all strange forms, and turn it into a single format. – seinecle Feb 23 '12 at 11:10
  • the strange forms you talk about, at least the user know what they are right ? so you can ask him for that format, create a SimpleDateformat with it and parse the date, then you use another SimpleDateFormat object to format the date to the desired format. – Fredszaq Feb 23 '12 at 11:18