Date format in jsp

試著忘記壹切 提交于 2020-01-07 09:03:34

问题


I need to convert the date from request parameter to string in dateformat 'yyyy-MM-dd'.

I have tried the following

String MyDate = request.getParameter("DayCal");
formatdate = new java.text.SimpleDateFormat("yyyy/MM/dd");
Date date = (Date) formatdate.parse(MyDate);
String DisplayDate= formatdate.format(date);

But i am getting incorrect results if the request.getParameter("DayCal") = 01/01/2010; the DisplayDate = 0006/07/03

Please help... Thanks in advance


回答1:


You will need two SimpleDateFormats - one for parsing and one for formatting:

String MyDate = request.getParameter("DayCal");
SimpleDataFormat parseDate = new java.text.SimpleDateFormat("dd/MM/yyyy");
SimpleDataFormat formatDdate = new java.text.SimpleDateFormat("yyyy-MM-dd");
Date date = (Date) parseDate.parse(MyDate);
String DisplayDate= formatDate.format(date);

(I added dashes instead of slashes, because that was your initial requirement).




回答2:


From your code, request.getParameter() returns date as dd/MM/YYYY or mm/dd/yyyy, but your SimpleDateFormat() is given yyyy/MM/dd parameters which doesnot match MyDate. Instead SimpleDateFormat() should be given dd/mm/yyyy or mm/dd/yyyy.



来源:https://stackoverflow.com/questions/2011206/date-format-in-jsp

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