Reputation: 978
I have a date which is a String that I get from a database.
"12-12-2013" // for example
I know how to convert this in java to a date type or change the format in any way I want, there are tones of examples out there. But the thing I can't find is:
I want to be able to find out which pattern the date-string is using I mean if is
"dd-MM-yyyy"
//or
"yyyy-MM-dd"
or something else.
Is there any example I don't know of or didn't find?
I need this because I have this function to change the string's format:
public static String formatMydate(String datestring, String previousformat, String newformat) {
java.util.Date date= null;
String response = "";
SimpleDateFormat formater;
if (datestring.length()>=10){
try {
formater=new SimpleDateFormat(previousformat);
date = formater.parse(datestring);
response = new SimpleDateFormat(newformat).format(date);
} catch (Exception e) {
e.printStackTrace();
date=null;
}
}
return response.toString();
}
But I want to know, before using it, the "previousformat" to enter it in the function parameter.
Is there a way to know which pattern a date is using?
This a duplicate question:
Returning a date format from an unknown format of date string in java [duplicate]
Upvotes: 1
Views: 809
Reputation: 77177
There's no simple general answer, since some places use dd-MM-yyyy
and some use MM-dd-yyyy
. If you only have to distinguish between dd-MM-yyyy
and yyyy-MM-dd
, split on the hyphen and count the digits.
Upvotes: 1