先把字符串日期转换成对应的格式,然后再转换成日期
public static void main(String args[]){
String str="2010/05/06";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
str=str.replace("/", "-");
System.out.println("str="+str);
System.out.println("time="+sdf.parse(str));
System.out.println("formateTime="+sdf.format(sdf.parse(str)));
} catch (ParseException e) {
e.printStackTrace();
}
}
结果:
str=2010-05-06
time=Thu May 06 00:00:00 CST 2010
formateTime=2010-05-06