If input is 01-01-2015
it should change to 2015-01-01
.
If input is 2015-01-01
it should change to 01-01-2015
.
I used SimpleDateFormat
but didn't get the correct output:
//Class to change date dd-MM-yyyy to yyyy-MM-dd and vice versa
public class ChangeDate {
static SimpleDateFormat formatY = new SimpleDateFormat("yyyy-MM-dd");
static SimpleDateFormat formatD = new SimpleDateFormat("dd-MM-yyyy");
//This function change dd-MM-yyyy to yyyy-MM-dd
public static String changeDtoY(String date) {
try {
return formatY.format(formatD.parse(date));
}
catch(Exception e) {
return null;
}
}
//This function change yyyy-MM-dd to dd-MM-yyyy
public static String changeYtoD(String date) {
try {
return formatD.format(formatY.parse(date));
}
catch(Exception e) {
return null;
}
}
}
I want some condition that automatically detects the date's pattern and change to the other format.