请问jsp下如何将字符串类型转换成日期型,请给写个简单例子

解决方案 »

  1.   

    <%      SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
            Date date=format.parse("2005-10-20");
            System.out.println(date);%>
      

  2.   

    <%
    String values = "2005-02-25";
    int year = Integer.parseInt(values.substring(0,4));
    int month = Integer.parseInt(values.substring(5,7));
    int day = Integer.parseInt(values.substring(8,10));
    Date value = new Date(year,month,day);
    out.println(value.getYear());
    out.println(value.getMonth());
    out.println(value.getDate());
    %>
      

  3.   

    /**
         * 
         * @param bYmd
         *            String
         * @return Date
         * @throws Exception
         *             ex
         */
        private Date stringToTimestamp(String bYmd) throws Exception {
            Date d = null;
            try {
                if (bYmd == null || bYmd.trim().length() == 0) {
                    return t;
                } else {
                     if (bYmd.length() == 8) {
                        bYmd = bYmd.substring(0, 4) + "-" + bYmd.substring(4, 6)
                                + "-" + bYmd.substring(6) 
                    }
                    bYmd = bYmd.replace('/', '-');
                    SimpleDateFormat sdf = new SimpleDateFormat(
                            "yyyy-mm-dd");
                     d = sdf.parse(bYmd);
                }
            } catch (Exception e) {
                throw e;
            }
            return d;
        }
      

  4.   

    为什么总是提示
    The type Date is ambiguous
      

  5.   

    private static Date stringToTimestamp(String bYmd) throws Exception {
            Date d = null;
            try {
                if (bYmd == null || bYmd.trim().length() == 0) {
                    return d;
                } else {
                     if (bYmd.length() == 8) {
                        bYmd = bYmd.substring(0, 4) + "-" + bYmd.substring(4, 6)
                                + "-" + bYmd.substring(6);
                    }
                    bYmd = bYmd.replace('/', '-');
                    SimpleDateFormat sdf = new SimpleDateFormat(
                            "yyyy-mm-dd");
                     d = sdf.parse(bYmd);
                }
            } catch (Exception e) {
                throw e;
            }
            return d;
        }
    没有问题的我测试都通过了。
      

  6.   

    楼上的,我知道在java下它没有问题,但是在jsp下总是提示The type Date is ambiguous
      

  7.   

    jsp和java下都应该没有问题的,你可以把它写成bean来用,把参数传给它不就可以了吗?
    要不你把你的代码贴出来看看.