01-01-2015
,它应该更改为2015-01-01
。如果输入为
2015-01-01
,它应该更改为01-01-2015
。我使用了
SimpleDateFormat
,但没有得到正确的输出结果://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;
}
}
}
我希望有一个自动检测日期格式并转换为其他格式的条件。