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.
  • 81,772
  • 15
  • 137
  • 161
  • 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

1 Answers1

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,924
  • 15
  • 23