Convert String dd/MM/yyyy date into java.sql.date yyyy-MM-dd

给你一囗甜甜゛ 提交于 2020-02-25 07:13:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!