0

I have a date in String Object (ex:(on, 22 maj, 2019)) , the locale date of the string is not known . As you can see it is in swedish locale but I don't know about it in runtime , I just have the string at my disposal. How can I convert first parse this string and then convert it into particular known locale for ex : German or even english.

Tried with Dateformatter and SimpleDateFormat in java but couldn't able to solve the problem

Ole V.V.
  • 76,217
  • 14
  • 120
  • 142
  • You can't really. You could try every locale, but it's possible 2 different locale's would both work and give different results. For example, `2-1-2017`, is that 2 january (UK) or is it 1 february (US)? – john16384 May 23 '19 at 07:52
  • Your string doesn’t follow any of the Swedish formats from the locale repository. That would have been either `onsdag 22 maj 2019` or just `22 maj 2019`. This makes the task almost impossible. Some heuristics and non-ignorable coding effort may get you close enough. – Ole V.V. May 23 '19 at 09:39
  • One example date string will get you nowhere. You will need a vast collection of diverse string objects so you can analyse any patterns in them and design an algorithm based on the knowledge you gain through that analysis. – Ole V.V. May 23 '19 at 10:06

2 Answers2

0

This is very difficult. For example different locales sort month and day differently. If you have a 'pure number' date, as "20.10.2016" for example, you don't for sure in what order month and date are (you can usually identify the year, as that is commonly 4 digits, but it can be 2-digits as well). So there are cases, where a date's locale cannot be uniquely identified.

If you have edge information, as for example, there is a specific set of locales that could match, or that, given a certain likelyhood for what locate it is, you could try to interpret them. However, you'd not be able to be absolutely certain you got the right interpretation.

My suggestion is to write regular expressions to identify the date format and then use SimpleDateFormat to parse the dates. Then use the 'chain of responsibility' pattern to try through all regular expressions, and if you got a match, you can use that particular SimpleDateFormat.

TreffnonX
  • 2,784
  • 14
  • 22
-2

I once had a task to write a code that would parse a String to date where date format was not known in advance. I.e. I had to parse any valid date format. I wrote a project and after that I wrote an article that described the idea behind my implementation. Here is the link to the article: Java 8 java.time package: parsing any string to date. General Idea is to write all the patterns that you wish to support into external properties file and read them from there and try to parse your String by those formats one by one until you succeed or run out of formats. Note that the order also would be important as some Strings may be valid for several formats (US/European differences). Advantage is that you can keep adding/removing formats to the file without changing your code. So such project could also be customized for different customers

Michael Gantman
  • 5,975
  • 1
  • 17
  • 34
  • How do you know if 07-08 is August 7th or July 8th? How do you know if 'Maj' is the full name of a month or an abbreviation of a month name? – Joakim Danielson May 23 '19 at 08:37
  • @Joakim Danielson - my proposal gives very good solution to your question. If you work at environment where most of your dates are expected to be US style then you place US format (...MM-dd...) before European (...dd-MM...) and then 07-08 will be interpreted as July 8th but 31-8 will be caught by European format and also interpreted as Aug 31. If you work in European style environment you just switch those formats by order and the system will work. However, it is simply impossible to say with 100% if 07-08 is August 7th or July 8th. – Michael Gantman May 23 '19 at 09:21
  • 1
    "...most of your dates are expected...", this is the problem isn't it? At best your solution is a good guess. – Joakim Danielson May 23 '19 at 09:33
  • Joakim when provided with String 07-08 without additional info it is simply technically impossible to say which one it is. So making an educated guess IS the best you can do. My solution provides not only a way to to make a guess but adjust it in time or per site without making any changes to code. This solution has been used in production in very heavily used product and more then proved itself. If you have a way to provide better solution then please share it with us. Otherwise you are criticizing proposal for not resolving un-resolvable problem – Michael Gantman May 23 '19 at 09:40
  • If you aren’t sure, it’s much better to give up and report that you can’t decide than risk giving an incorrect result and having the user believe that it is correct. If your user interface allows for it, you may present both options and let the user choose. Making a guess, however educated, is the worst thing you can do IMHO. – Ole V.V. May 23 '19 at 10:13
  • @Ole - With all due respect I strongly disagree. The dilema on how to interpret 07-08 is theoretically unsolvable, so you'd be answering "I don't know" all the time. My solution provides an option to user or admin to set as a parameter which format US or European to set as default. So my proposed solution allows user to control how to choose in advance in questionable scenario, plus you can change your decision just by changing param. Like I said this solution has been used in production and proved itself to be very flexible. – Michael Gantman May 23 '19 at 11:07