How to convert String to Date without knowing the String format?

advertisements

In My Application I am reading the date parameter from user in the String format.

After reading the parameter, I need to convert the String to *desired date format.*

Desired format = YYYY-MM-DD HH:MM:SS

Input String can be in any format.

Eg: 2013/09/19 14:21:07 OR 2013-09-19 14:21:07

Irrespective of the format Obtained, I need to convert it to desired format. How to achieve this?

I saw few snippet where,

SimpleDateFormat formatter = new SimpleDateFormat("YYYY/MM/DD HH:MM:SS");
Date dt = formatter.parse("2013/09/19 14:21:07");
SimpleDateFormat desiredFormatter = new
SimpleDateFormat("YYYY-MM-DD HH:MM:SS");

desiredFormatter .format(dt);

The above snippet works only when we know the input format to parse the String. But In my case I dont know the input format. So I was thinking to use directly format method without parsing.

Is this possible?


It's not possible. You cannot determine if 01/01/2013 is MM/dd/yyyy or dd/MM/yyyy if you dont know the real format beforehand