Create a method in a Global Class From where you can access this method in every class in Android(Java).
public static String convertDateFormat(String inputFormat, String outPutFormat) {
List<SimpleDateFormat> knownPatterns = new ArrayList<SimpleDateFormat>();
knownPatterns.add(new SimpleDateFormat("dd/MM/yyyy"));
knownPatterns.add(new SimpleDateFormat("yyyy-MM-dd"));
knownPatterns.add(new SimpleDateFormat("yyyy-MM-dd HH:MM:SS"));
knownPatterns.add(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"));
DateFormat outputFormat = new SimpleDateFormat(outPutFormat);
Date date = null;
for (int i = 0; i < knownPatterns.size(); i++) {
try {
date = knownPatterns.get(i).parse(inputFormat);
} catch (ParseException e) {
e.printStackTrace();
}
}
return outputFormat.format(date);
}
As I have Created method convertDateFormat in Java class Utility.java as shown above.
Now Call this method in the main class where you want to format date
Utility.convertDateFormat(userProfile.getDateOfBirth(), "dd/MM/yyyy")
Now, here Utility is java class where i have made method and i am calling that method in class where i want to convert Date Format.
Note:-where (userProfile.getDateOfBirth()) is input date which we want to convert and ( "dd/MM/yyyy") is output format date which you can give any format which is shown in method above.for another format which is not in method you can add that format in method.
public static String convertDateFormat(String inputFormat, String outPutFormat) {
List<SimpleDateFormat> knownPatterns = new ArrayList<SimpleDateFormat>();
knownPatterns.add(new SimpleDateFormat("dd/MM/yyyy"));
knownPatterns.add(new SimpleDateFormat("yyyy-MM-dd"));
knownPatterns.add(new SimpleDateFormat("yyyy-MM-dd HH:MM:SS"));
knownPatterns.add(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"));
DateFormat outputFormat = new SimpleDateFormat(outPutFormat);
Date date = null;
for (int i = 0; i < knownPatterns.size(); i++) {
try {
date = knownPatterns.get(i).parse(inputFormat);
} catch (ParseException e) {
e.printStackTrace();
}
}
return outputFormat.format(date);
}
As I have Created method convertDateFormat in Java class Utility.java as shown above.
Now Call this method in the main class where you want to format date
Utility.convertDateFormat(userProfile.getDateOfBirth(), "dd/MM/yyyy")
Now, here Utility is java class where i have made method and i am calling that method in class where i want to convert Date Format.
Note:-where (userProfile.getDateOfBirth()) is input date which we want to convert and ( "dd/MM/yyyy") is output format date which you can give any format which is shown in method above.for another format which is not in method you can add that format in method.