I am trying to convert a date string object to a date in java regardless of the format of the current system date. Because I want my custom date format to be stored in the database. Below is my code and please consult me.
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) {
For SimpleDateFormat, it can only parse the format I heard.
dateToString("16/04/2015");
It can convert for the above code. But, when I try in the following format
dateToString("Thursday, April 16, 2015");
I am coming Nepalese date: error Thursday, April 16, 2015.
java string
Thiha zaw
source share