2012-11-21 60 views
2

這裏是我的代碼:代碼是用於獲取服務器端的值,所以我想將日期轉換爲字符串格式,然後我所說的數據在腳本文件我想將日期轉換爲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> 
+1

你的問題不清楚 – Abubakkar

+0

你的問題到底是什麼?你使用SimpleDateFormat將日期轉換爲一個字符串,這樣做可以(除了可以使formatDateJava成爲一個靜態成員變量而不是一遍又一遍地創建它)。 – Ridcully

+0

@senthamizh:你想用Javascript做什麼?您的帖子中尚不清楚。請提供更多詳細信息 – Ramya

回答

0

看來你如果條件是錯誤的

嘗試

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

與其使用

if (request.getParameter("today").equals("dateNow")) 
0

使用AJAX用於從JavaScript調用此方法。您可以使用JSON對象以方便使用。 防爆AJAX: $ .getJSON(URL,功能(數據){

}

你會得到你的數據串像{[您的字符串]}

而對於比較日期即可。使用dateNow.after(anyDate)和dateNow.before(anyDate)和dateNow.equals(anyDate)易於使用

0

看一下代碼:

Date dateNow = new Date(); 
... 
if (request.getParameter("today").equals("dateNow")) 
  • request.getParameter()返回String,比如返回「2012年11月20日」
  • 接下來檢查if(「2012年11月20日」.equals(「dateNow」)),它永遠不會是真的

我猜你的目的是檢查:

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

而且,不要忘了修復:

if(request.getParameter("today").equals("dateNow-2")) 
0

試着去了解FOLL由於代碼,希望你會得到你的答案...

    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); 
      } 
     } 
+0

我的問題是點擊最近7天數據的7次按鈕顯示,在我的html編碼中,我把ajax編碼從服務器端獲得價值請回答我 – senthamizh

相關問題