| |
20. 20. 16. Convert a String Date Such as 2003/01/10 to a java.sql.Date Object? |
|
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class MainClass {
public static void main(String[] args) throws ParseException {
try {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
String date = "2003/01/10";
java.util.Date utilDate = formatter.parse(date);
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
System.out.println("date:" + date);
System.out.println("sqlDate:" + sqlDate);
} catch (ParseException e) {
System.out.println(e.toString());
e.printStackTrace();
}
}
}
|
|
date:2003/01/10
sqlDate:2003-01-10 |
|