6

Possible Duplicate:
Parse any date in Java

Suppose we have a string of date in a format (unknown to the user). Examples of acceptable dates that you can receive from a user:

  • yyyy-MM-dd/yy-MM-dd
  • yyyy/MM/dd/yy/MM/dd
  • dd/MM/yyyy/dd/MM/yy
  • MM/dd/yyyy/MM/dd/yy`

Is there a library that accept a date in a string and returns a date format that can be used by SimpleDateFormat or Joda Time?

Thanks

Community
  • 1
  • 1
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
  • 6
    would be interesting to see how a solution could accomodate 09/08/2010 ... how would you know which is month and which is day as both are perfectly acceptable dependent on the user's region – sdolgy Jun 23 '11 at 08:00
  • 3
    Did you see the question [Parse any date in Java](http://stackoverflow.com/q/3389348/127035) and answers. Not a simple library, but code for your own utility based on pattern matching and then lookup. – ewan.chalmers Jun 23 '11 at 08:07
  • @sdolgy, brilliant question. One should ask the user if that's the correct date it parsed then. I think user should accept which result is correct. – Buhake Sindi Jun 23 '11 at 08:08
  • That question (month or day) is addressed in the other question I linked to also. – ewan.chalmers Jun 23 '11 at 08:10
  • `dd/MM/yyyy` is a very uncommon format (I believe it's even not official). Usually `-` is been used as separator for such pattern. – BalusC Jun 23 '11 at 09:06
  • @BalusC, yes, but `DateFormat` can allow parsing of dates with that specified format. I'm just allowing the possibility someone wants to have it. – Buhake Sindi Jun 23 '11 at 09:09
  • Why would you support an uncommon format? – BalusC Jun 23 '11 at 09:15
  • Because that's the requirement for this project, I'm afraid. And no, I wasn't there when they debated about this. *cough - Business Analysts*. – Buhake Sindi Jun 23 '11 at 09:17

1 Answers1

0

As a simple solution: I will apply each date format on the provided input to get a Date object , then I print the Date object with the same date format. If the input and the output for a date format are the same the date format is a candidate.

salman.mirghasemi
  • 1,009
  • 2
  • 7
  • 15
  • 1) That is a long way and object-initialization and comparison technique that is unecessary. 2) That doesn't solve the issue of whether `03/04/2010` means 3 April or 4th March. – Buhake Sindi Jun 23 '11 at 09:02
  • What you are saying contradicts what you explained in your comment. You said that user should decide about the acceptable result. – salman.mirghasemi Jun 23 '11 at 09:04
  • @salman.mirghasemi, fair enough, what I'm saying, if I add 3 April and his solution finds 4 April, his output versus date object comparison check stands valid, there's no way of showing the ambiguous case and notify the user. – Buhake Sindi Jun 23 '11 at 09:12
  • User gets these two: DD/MM/YYYY and MM/DD/YYYY, then chooses one. If you want to make it more user friendly you can show each case in long format by asking this question: "Which one do you mean: 3 April 2010 or 4 March 2010?" – salman.mirghasemi Jun 23 '11 at 09:17
  • Pretty much or create a validation that proposes it. Also, the answer doesn't solve the problem of finding an "appropriate" date format based on the date string. It assumes I have a list of date formats defined (which can be long as there are some format that depends on regions and locales, etc.). – Buhake Sindi Jun 23 '11 at 09:29
  • Even in that case, having a list of possible date formats (though that is not complete) can improve the quality of your inference. – salman.mirghasemi Jun 23 '11 at 09:36