0

I have 2 jsp page, i sent value from frist page to second page, but when i show value in input tag, the value have changed. What wrong with my coded? The first page:

<script>
    $(function() {
        $( "#datepicker" ).datepicker({
            dateFormat: "yy/mm/dd",
            showOn: "focus"
        });
    });
    </script>

JSP:

<p>Date: <input type="text" id="datepicker" name="datepicker"></p>

And Servlet:

HttpSession s= request.getSession();
String olddate= (String)request.getParameter("datepicker");
s.setAttribute("DATE", olddate);    
RequestDispatcher rd = request.getRequestDispatcher("nextpage.jsp");

The second Page JSP:

<%
HttpSession s= request.getSession();
DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
String date = s.getAttribute("DATE").toString();
Date dateto = sdf.parse(date);
String newDateString = sdf.format(dateto);
    %>
<p>Date From: <input type="text" id="datefrompicker" readonly="readonly">/p>

And Script:

$(document).ready(function(){
        var date =<%=newDateString%>;
        alert(dateto);
        $( "#datefrompicker" ).datepicker( "setDate", date );       
    });

The first page value is 2015/11/01,the value in alert is 91.5909090909091, and input datefrompicker don't have any thing.

What am i do wrong?

thefriend
  • 339
  • 1
  • 6
  • 16

1 Answers1

0

In first code section dateFormat: "yy/mm/dd", the year format in the alert date string yyyy, so use SimpleDateFormat to format accordingly.

The Date.toString method returns date string which cannot be parsed by DatePicker.setDate method.

Example:

$( ".selector" ).datepicker( "setDate", "10/12/2012" );

Please use the SimpleDateFormat to format the date string and everything should work fine.

rajuGT
  • 6,224
  • 2
  • 26
  • 44