這裏是我的代碼:代碼是用於獲取服務器端的值,所以我想將日期轉換爲字符串格式,然後我所說的數據在腳本文件我想將日期轉換爲java.lang中的字符串,但我需要在javascript中調用日期的編碼
**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>
你的問題不清楚 – Abubakkar
你的問題到底是什麼?你使用SimpleDateFormat將日期轉換爲一個字符串,這樣做可以(除了可以使formatDateJava成爲一個靜態成員變量而不是一遍又一遍地創建它)。 – Ridcully
@senthamizh:你想用Javascript做什麼?您的帖子中尚不清楚。請提供更多詳細信息 – Ramya