new Date(String);
或者用java.text.SimpleDateFormat

解决方案 »

  1.   

    用new Date(String)在Servlet中是不允许的
      不知后者如何使用
      

  2.   

    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.
      

  3.   

    可以了,谢谢sparkwu , 谢谢airlulu