string转date能远行,但是会报错
能正常执行输出正确的结果,但是问题是他还会报错,这是为什么啊?
import java.sql.Date;
import
java.text.ParseException;
import java.text.SimpleDateFormat;
public class TestData2 {
//DATE_TIME_FORMAT
public static SimpleDateFormat format = new SimpleDateFormat();
public static Date date = new Date(System.currentTimeMillis());
public static void main(String[] args){
Date date = new Date(System.currentTimeMillis());
System.out.println(convertDate2String(date,"yyyy-MM-dd"));
System.out.println(convertString2Date("2001.12.12-08.23.21","yyyy.MM.dd"));
}
private static String convertDate2String(Date date, String formatDate) {
format.applyPattern(formatDate);
String s = format.format(date);
return s;
}
private static Date convertString2Date(String value, String formatDate) {
SimpleDateFormat sdf = new SimpleDateFormat(formatDate);
String s= value.substring(0,10); //截取出年月日
System.out.println(s);
try {
Date date = (Date) sdf.parse(s);//字符串转时间
} catch (
ParseException e) {
e.printStackTrace();
}
return date;
}
}
------解决方案--------------------把import java.sql.Date;修改为import java.util.Date;
------解决方案--------------------同意二楼的~你把Date包给引错了~所以会出现错误~