2

这是我的代码:该代码用于在服务器端获取值,所以我想将日期转换为字符串格式,然后我在脚本文件中调用数据

**My Servlet code**


package pack.com;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class project extends HttpServlet {
    private String date_to_string;
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        Date dateNow = new Date();
        SimpleDateFormat formatDateJava = new SimpleDateFormat("MM/dd/yyyy");
        date_to_string = formatDateJava.format(dateNow);
        System.out.println("Today's date into MM/dd/yyyy format: " + date_to_string);
        StringBuilder sb = null;
        PrintWriter out = response.getWriter();
        sb = new StringBuilder();
        if (request.getParameter("today").equals("dateNow"))
        {
            sb.append("[");
            sb.append("{\"Stack_No\":\"13\",\"Buyer_Name\":\"Vickram\",\"No_of_Trips\":\"34\",\"Dispatched_Quantity\":\"56\"},");
            sb.append("{\"Stack_No\":\"04\",\"Buyer_Name\":\"sivaji\",\"No_of_Trips\":\"34\",\"Dispatched_Quantity\":\"45\"},");
            sb.append("{\"Stack_No\":\"25\",\"Buyer_Name\":\"mathavan\",\"No_of_Trips\":\"82\",\"Dispatched_Quantity\":\"34\"}");
            sb.append("]");
        } else if(request.getParameter("today").equals("dateNow-1")){
            sb.append("[");
            sb.append("{\"Stack_No\":\"06\",\"Buyer_Name\":\"karthik\",\"No_of_Trips\":\"28\",\"Dispatched_Quantity\":\"23\"},");
            sb.append("{\"Stack_No\":\"4\",\"Buyer_Name\":\"Murali\",\"No_of_Trips\":\"29\",\"Dispatched_Quantity\":\"8\"},");
            sb.append("{\"Stack_No\":\"6\",\"Buyer_Name\":\"MGR\",\"No_of_Trips\":\"10\",\"Dispatched_Quantity\":\"8\"}");
            sb.append("]");
        }else if(request.getParameter("today").equals("dateNow-2")){
            sb.append("[");
            sb.append("{\"Stack_No\":\"06\",\"Buyer_Name\":\"karthik\",\"No_of_Trips\":\"28\",\"Dispatched_Quantity\":\"23\"},");
            sb.append("{\"Stack_No\":\"4\",\"Buyer_Name\":\"Murali\",\"No_of_Trips\":\"29\",\"Dispatched_Quantity\":\"8\"},");
            sb.append("{\"Stack_No\":\"6\",\"Buyer_Name\":\"MGR\",\"No_of_Trips\":\"10\",\"Dispatched_Quantity\":\"8\"}");
            sb.append("]");

    }else if(request.getParameter("today").equals("dateNow-3")){
        sb.append("[");
        sb.append("{\"Stack_No\":\"13\",\"Buyer_Name\":\"Vickram\",\"No_of_Trips\":\"34\",\"Dispatched_Quantity\":\"56\"},");
        sb.append("{\"Stack_No\":\"04\",\"Buyer_Name\":\"sivaji\",\"No_of_Trips\":\"34\",\"Dispatched_Quantity\":\"45\"},");
        sb.append("{\"Stack_No\":\"25\",\"Buyer_Name\":\"mathavan\",\"No_of_Trips\":\"82\",\"Dispatched_Quantity\":\"34\"}");
        sb.append("]");
    }
    else if(request.getParameter("today").equals("dateNow-4")){
        sb.append("[");
        sb.append("{\"Stack_No\":\"13\",\"Buyer_Name\":\"Vickram\",\"No_of_Trips\":\"34\",\"Dispatched_Quantity\":\"56\"},");
        sb.append("{\"Stack_No\":\"04\",\"Buyer_Name\":\"sivaji\",\"No_of_Trips\":\"34\",\"Dispatched_Quantity\":\"45\"},");
        sb.append("{\"Stack_No\":\"25\",\"Buyer_Name\":\"mathavan\",\"No_of_Trips\":\"82\",\"Dispatched_Quantity\":\"34\"}");
        sb.append("]");
    }
    else if(request.getParameter("today").equals("dateNow-5")){
        sb.append("[");
        sb.append("{\"Stack_No\":\"13\",\"Buyer_Name\":\"Vickram\",\"No_of_Trips\":\"34\",\"Dispatched_Quantity\":\"56\"},");
        sb.append("{\"Stack_No\":\"04\",\"Buyer_Name\":\"sivaji\",\"No_of_Trips\":\"34\",\"Dispatched_Quantity\":\"45\"},");
        sb.append("{\"Stack_No\":\"25\",\"Buyer_Name\":\"mathavan\",\"No_of_Trips\":\"82\",\"Dispatched_Quantity\":\"34\"}");
        sb.append("]");
    }
    else if(request.getParameter("today").equals("dateNow-6")){
        sb.append("[");
        sb.append("{\"Stack_No\":\"06\",\"Buyer_Name\":\"karthik\",\"No_of_Trips\":\"28\",\"Dispatched_Quantity\":\"23\"},");
        sb.append("{\"Stack_No\":\"4\",\"Buyer_Name\":\"Murali\",\"No_of_Trips\":\"29\",\"Dispatched_Quantity\":\"8\"},");
        sb.append("{\"Stack_No\":\"6\",\"Buyer_Name\":\"MGR\",\"No_of_Trips\":\"10\",\"Dispatched_Quantity\":\"8\"}");
        sb.append("]");

    }
        out.println(sb.toString());
        out.flush();
        out.close();
    }

}

上面的代码是将日期转换为字符串,但我不确定它是否正确。

html编码

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />

        <!-- Always force latest IE rendering engine (even in intranet) & Chrome Frame
        Remove this if you use the .htaccess -->
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />

        <title>index</title>
        <meta name="description" content="" />
        <meta name="author" content="nura" />

        <!--<meta name="viewport" content="width=device-width; initial-scale=1.0" /> -->
        <!-- CSS -->
        <link rel="stylesheet" href="css/table.css" type="text/css" media="screen" title="no title" charset="utf-8"/>
        <!-- Replace favicon.ico & apple-touch-icon.png in the root of your domain and delete these references -->
        <link rel="shortcut icon" href="/favicon.ico" />
        <link rel="apple-touch-icon" href="/apple-touch-icon.png" />
        <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
        <script type="text/javascript" charset="utf-8">
            $(document).ready(function() {
                $.ajax({
                type : "GET",
                url : yt_url,
                data : "servlet_id=1&prop_id=" + propId,
                async : false,
                success : function(response) {
                    if (response.data) {
                    }
                }
            });

                    $("#j").text(json[0].Stack_No);
                    $("#i").text(json[0].Buyer_Name);
                    $("#h").text(json[0].No_of_Trips);
                    $("#m").text(json[0].Dispatched_Quantity);
                    $("#g").text(json[1].Stack_No);
                    $("#f").text(json[1].Buyer_Name);
                    $("#e").text(json[1].No_of_Trips);
                    $("#l").text(json[1].Dispatched_Quantity);
                    $("#c").text(json[2].Stack_No);
                    $("#b").text(json[2].Buyer_Name);
                    $("#a").text(json[2].No_of_Trips);
                    $("#k").text(json[2].Dispatched_Quantity);
                    $("#n").text(json[3].Stack_No);
                    $("#o").text(json[3].Buyer_Name);
                    $("#p").text(json[3].No_of_Trips);
                    $("#q").text(json[3].Dispatched_Quantity);
                    $("#r").text(json[4].Stack_No);
                    $("#s").text(json[4].Buyer_Name);
                    $("#t").text(json[4].No_of_Trips);
                    $("#u").text(json[4].Dispatched_Quantity);
                    $("#v").text(json[5].Stack_No);
                    $("#w").text(json[5].Buyer_Name);
                    $("#x").text(json[5].No_of_Trips);
                    $("#y").text(json[5].Dispatched_Quantity);
                });


        </script>

    </head>
    <body>
        <img id="ajaxLoader" src="image/logo.jpg" style="display: none;" />

        <div id="header">
            <h2><b>Daily Despath Report</b></h2>
        </div>
        <div id="container">
            <h1>Mine:-------<h1>
        </div>
        <body>
            <div id="time">
            <span id=tick2>
</span>
<script>
function show2(){
if (!document.all&&
!document.getElementById)
return
thelement=document.getElementById? document.getElementById("tick2"): document.all.tick2
var Digital=new Date()
var hours=Digital.getHours()
var minutes=Digital.getMinutes()
var seconds=Digital.getSeconds()
var dn="PM"
if (hours<12)
dn="AM"
if (hours>12)
hours=hours-12
if (hours==0)
hours=12
if (minutes<=9)
minutes="0"+minutes
if (seconds<=9)
seconds="0"+seconds
var ctime=hours+":"+minutes+":"+seconds+" "+dn
thelement.innerHTML="<b style='font-size:14;color:blue;'>"+ctime
+"</b>"
setTimeout("show2()",1000)
}
window.onload=show2
</script>
<div id="my">
<script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
<script type="text/javascript">
var dateNow = new Date();
var month = dateNow.getMonth()+1;
var day = dateNow.getDate();
var year = dateNow.getFullYear();
var date = month + " /" + day + " /" + year;
function ShowSchedule() 
{
$('#today').val(date);
}
function ShowDay(temp,isNextDay)
{
if(isNextDay)
{
datecount=$('#'+temp).val();
if(datecount>0){
var tempval=--datecount;
var cur_dat=new Date();
curdate=cur_dat.getDate();
var diff_dat=new Date();
diff_dat.setDate(curdate-tempval);
$('#today').val(diff_dat.getMonth()+1+" /"+diff_dat.getDate()+" /"+diff_dat.getFullYear());
$('#temp').val(tempval);
}

else{
alert("Data is available for up to today date only");
}
}
else{
datecount=$('#'+temp).val();
if(datecount<=6){
var tempval=++datecount;
var cur_dat=new Date();
curdate=cur_dat.getDate();
var diff_dat=new Date();
diff_dat.setDate(curdate-tempval);
$('#today').val(diff_dat.getMonth()+1+" /"+diff_dat.getDate()+" /"+diff_dat.getFullYear());
$('#temp').val(tempval);
}else{
alert("Data is not available for more than 7 days from current date");
}
}
}
</script>
</div>
</div>
<a id = "prev" href="#" onClick="ShowDay('temp',false)">Prev</a>
<input type="hidden" id="temp" value="0">
<input type="text" id="today" size="10">
<a id = "next" href="#" onClick="ShowDay('temp',true)">Next</a> 
<input type="hidden" id="temp" value="i">  
<script>ShowSchedule();
</script>

</body>

        <div id="CSSTableGenerator" >
            <table >
                <tr>
                    <td><i>Materials</i></td>
                    <td><i>Stack No</i></td>
                    <td><i>Buyer Name</i></td>
                    <td ><i>No of Trips</i></td>
                    <td><i>Dispatched Quantity</i></td>
                </tr>

                <tr>
                    <td>Screened Fines</td>
                    <td id="j"></td>
                    <td id="i"></td>
                    <td id="h"></td>
                    <td id="m"></td>

                </tr>

                <tr>
                    <td>Screened 5-18mm</td>
                    <td id="g"></td>
                    <td id="f"></td>
                    <td id="e"></td>
                    <td id="l"></td>
                </tr>
                <tr>
                    <td>Screened 10-30mm</td>
                    <td id="c"></td>
                    <td id="b"></td>
                    <td id="a"></td>
                    <td id="k"></td>
                </tr>
                <tr>
                    <td>Screened Lumps</td>
                    <td id="n"></td>
                    <td id="o"></td>
                    <td id="p"></td>
                    <td id="q"></td>
                </tr>
                <tr>
                    <td>Crushed Fines</td>
                    <td id="r"></td>
                    <td id="s"></td>
                    <td id="t"></td>
                    <td id="u"></td>
                </tr>
                <tr>
                    <td>Crushed 5-18mm</td>
                    <td id="v"></td>
                    <td id="w"></td>
                    <td id="x"></td>
                    <td id="y"></td>
                </tr>
            </table>

        </div>
        <div id="button">
            <form>
                <br>
                <br>
                <b style="color:white"><i>Yesterday DDR</i></b>
                <br>

                <input type="button"
                style="
                background: -moz-linear-gradient(top,  rgb(255,183,107) 0%, rgb(255,167,61) 50%, rgb(255,124,0) 51%, rgb(255,127,4) 100%);
                background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgb(255,183,107)), color-stop(50%,rgb(255,167,61)), 
                color-stop(51%,rgb(255,124,0)), color-stop(100%,rgb(255,127,4)));
                background: -webkit-linear-gradient(top,  rgb(255,183,107) 0%,rgb(255,167,61) 50%,rgb(255,124,0) 51%,rgb(255,127,4) 100%);
                background: -o-linear-gradient(top,  rgb(255,183,107) 0%,rgb(255,167,61) 50%,rgb(255,124,0) 51%,rgb(255,127,4) 100%);
                background: -ms-linear-gradient(top,  rgb(255,183,107) 0%,rgb(255,167,61) 50%,rgb(255,124,0) 51%,rgb(255,127,4) 100%);
                background: linear-gradient(to bottom,  rgb(255,183,107) 0%,rgb(255,167,61) 50%,rgb(255,124,0) 51%,rgb(255,127,4) 100%);
                "onClick="location.href='nura.html'" value='Click'>
                <a>
                <input type="button"
                style="
                background: -moz-linear-gradient(top,  rgb(255,183,107) 0%, rgb(255,167,61) 50%, rgb(255,124,0) 51%, rgb(255,127,4) 100%);
                background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgb(255,183,107)), color-stop(50%,rgb(255,167,61)), 
                color-stop(51%,rgb(255,124,0)), color-stop(100%,rgb(255,127,4)));
                background: -webkit-linear-gradient(top,  rgb(255,183,107) 0%,rgb(255,167,61) 50%,rgb(255,124,0) 51%,rgb(255,127,4) 100%);
                background: -o-linear-gradient(top,  rgb(255,183,107) 0%,rgb(255,167,61) 50%,rgb(255,124,0) 51%,rgb(255,127,4) 100%);
                background: -ms-linear-gradient(top,  rgb(255,183,107) 0%,rgb(255,167,61) 50%,rgb(255,124,0) 51%,rgb(255,127,4) 100%);
                background: linear-gradient(to bottom,  rgb(255,183,107) 0%,rgb(255,167,61) 50%,rgb(255,124,0) 51%,rgb(255,127,4) 100%);
                "onclick="location.href='datepicker.html'" value='Trend'>
                <br>
            </form>
        </div>
    </body>
</html>
4

4 回答 4

0

试着理解下面的代码,希望你能得到答案……

                    import java.text.SimpleDateFormat;
        import java.util.Calendar;
        import java.util.Date;

        public class Demo {

           public static void main(String[] args) {


              Calendar cal = Calendar.getInstance();




              Date today=cal.getTime();

              SimpleDateFormat formatDateJava = new SimpleDateFormat("MM/dd/yyyy");
              String today_string = formatDateJava.format(today);


           System.out.println("The current date is : " + today_string);

              cal.add(Calendar.DATE, -1);


              Date today_minus_1=cal.getTime();
              String today_minus_1_string=formatDateJava.format(today_minus_1);

              System.out.println(today_minus_1_string);
           }
        }
于 2012-11-21T09:56:28.350 回答
0

似乎你的 if 条件是错误的

尝试

if (request.getParameter("today").equals(date_to_string)) 

代替

if (request.getParameter("today").equals("dateNow"))
于 2012-11-21T07:18:25.420 回答
0

使用 AJAX 从 Javascript 调用此方法。您可以使用 JSON 对象以方便使用。Ex ajax: $.getJSON(url,function(data){

}

您将在 {[your string]} 等数据中获取您的字符串。

为了比较日期,您可以使用 dateNow.after(anyDate) 和 dateNow.before(anyDate) 和 dateNow.equals(anyDate) 以便于使用。

于 2012-11-21T07:22:37.693 回答
0

看代码:

Date dateNow = new Date();
...
if (request.getParameter("today").equals("dateNow"))
  • request.getParameter() 返回字符串,例如,它返回“11/20/2012”
  • 接下来你要检查 if("11/20/2012".equals("dateNow")),这永远不会是真的

我猜你的意图是检查:

if (request.getParameter("today").equals(date_to_string))

另外,不要忘记修复:

if(request.getParameter("today").equals("dateNow-2"))
于 2012-11-21T07:26:55.770 回答