I'm trying to convert a date string to date object in java regardless of current system date format. Because I want to get my custom date format to store in database. The following is my code and please advice me the way.
public static String dateToString(String date){
if(date.equals("")) return "";
if(date == null || date.length() == 0){
return "";
}
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
try {
Date l_date = format.parse(date);
Calendar calendar = Calendar.getInstance();
calendar.setTime(l_date);
String year = String.format("%04d", calendar.get(Calendar.YEAR));
String month = String.format("%02d", calendar.get(Calendar.MONTH));
String day = String.format("%02d", calendar.get(Calendar.DATE));
return year + month + day;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "";
}
}
For SimpleDateFormat, it can only parse the format I heard coded.
dateToString("16/04/2015");
It can convert for above code. But, when I try with the following format
dateToString("Thursday, April 16, 2015");
I go Unparseable date: "Thursday, April 16, 2015" error.