Thursday, 19 September 2013

How to Convert String to Date without knowing the format of String?

How to Convert String to Date without knowing the format of String?

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?

No comments:

Post a Comment