looking for a method which takes a list of dates(of string type) as inputs and returns possible date formats.
for example: input is ["01/02/2018","13/09/2018","8/11/2016"] then output should be "dd/MM/yyyy".
this is one of the example. it should be able to return all supported date formats by java which best suits for the given input.
I don’t think such a method is readily available. You will need to code it yourself.
That’s possible, though it may not always be possible to give a unique answer.
Require exactly three numbers in the string and the same separators before, between and after the numbers in each string. Pick up the first, the second and the third number from each string. Find the minimum and the maximum of each. I am taking your example:
["01/02/2018","13/9/2018","8/11/2016"]
The year is easy, that’s the number where min and max are within, say, 1900 through 2100. Choose limits that fit your situation. Month is the number that does not go above 12. In your example the second number is the month since the first has a max of 13. If none of the non-year numbers goes above 12, you can’t decide and will need to throw an exception or the like. Once you’ve decided year and month, the number you have not used, is the day of the month. Check that it is within 1 through 31 for validation.
For both day and month: If there is an occurrence with a leading zero (01
and 02
in your example) and all occurrences have two digits, require two digits in the format, using dd
or MM
. Otherwise use d
or M
(which will print enough digits for the number and will parse both one digit and two digit input). Because in your example month is given as 9
(one digit, in the second string) and 8
as day, use d
and M
.
Final result: d/M/uuuu
or d/M/yyyy
.