请问在Servlet中如何将String型的时间转换成Date型的时间

xjx802 2003-04-07 09:16:26
请问如何将Html提交的表单时间值,在servlet中转换成Date型的时间值
...全文
388 点赞 收藏 4
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
xjx802 2003-04-07
可以了,谢谢sparkwu , 谢谢airlulu
回复
sparkwu 2003-04-07
Try it:
StringtoDate.htm
<HTML>
<TITLE>StringtoDate</TITLE>
<HEAD>StringtoDate</HEAD>
<BODY>
Please input dateformat yyyy-mm-dd
<FORM ACTION="servlet/StringtoDate" METHOD="GET">
<INPUT NAME="sample" VALUE="">
<BUTTON TYPE="SUBMIT">
SUBMIT
</BUTTON>
</FORM>
</BODY>
</HTML>

Servlet StringtoDate.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class StringtoDate extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=Big5";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
String dateString = request.getParameter("sample");
SimpleDateFormat sdfInput = new SimpleDateFormat( "yyyy-MM-dd" );
SimpleDateFormat sdfOutput = new SimpleDateFormat ( "MM/dd/yyyy" );

Date date = null ;
try {
date = sdfInput.parse(dateString);
}
catch (Exception e)
{};

out.println("<html>");
out.println("<head><title>StringtoDate</title></head>");
out.println("<body bgcolor=\"#ffffff\">");
out.println( sdfOutput.format( date ) );
out.println("<p>The servlet has received a GET. This is the reply.</p>");
out.println("</body></html>");
}
//Clean up resources
public void destroy() {
}
}

if any question send message to me.
回复
xjx802 2003-04-07
用new Date(String)在Servlet中是不允许的
  不知后者如何使用
回复
airlulu 2003-04-07
new Date(String);
或者用java.text.SimpleDateFormat
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2003-04-07 09:16
社区公告
暂无公告