问题
I am using the below method to do the described conversion. But the java.sql.Date method is deprecated. So, is their any harm in using this method? As the statements below are working fine.
(Retrieving value of date as string in servlet from session and then storing the date in MySQL.)
String f33_ = (String) session.getAttribute("sf33");
java.sql.Date f33 = new java.sql.Date(Integer.parseInt(f33_.substring(6, 10))- 1900, Integer.parseInt(f33_.substring(3, 5))-1,Integer.parseInt(f33_.substring(0, 2)));
回答1:
You should use SimpleDateFormat (or the equivalent from Joda Time) rather than trying to parse this yourself:
DateFormat format = new SimpleDateFormat("dd/MM/yyyy", Locale.US);
format.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
java.util.Date date = format.parse(text);
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
来源:https://stackoverflow.com/questions/9624777/convert-string-dd-mm-yyyy-date-into-java-sql-date-yyyy-mm-dd