2012-08-04 26 views
-4

可能重複:
converting timestamp to date in java轉換時間戳至今在java和顯示計數值

這是我的數據庫:

  • 最新狀態

  • 1343993311 Q

  • 1343892452Ç
  • 1343892477 P

在這裏,我要檢查查詢當前日期+狀態= Q的信息,並得到計數值。

這是我的代碼:

public class TodayQ { 
public int data(){ 
int count=0; 

//count++; 

try{ 
    Class.forName("com.mysql.jdbc.Driver"); 
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/pro","root",""); 

    PreparedStatement statement = con.prepareStatement("select * from orders where status='Q' AND date=CURDATE()"); 
    ResultSet result = statement.executeQuery(); 
    while(result.next()) { 
      // Do something with the row returned. 
      count++; //if the first col is a count. 
     } 



    } 

     catch(Exception exc){ 
     System.out.println(exc.getMessage()); 
     } 


    return count; 
     } 

     } 

DIS是我的其他類:

public class Demo { 
    public static void main(String[] args) throws ParseException{ 
    TodayQ obj = new TodayQ(); 
    System.out.println(obj.data()); 

    } 

    } 

這裏我有編輯日期(YYYY-MM-DD)format.Now我得到了output.but我希望在我的數據庫上使用時間戳。如何轉換時間戳日期在java.How是使用我的code.please代碼幫助我。如何做?

+2

我想知道'訂單分配'在哪裏發生?相關的問題是[這裏](http://stackoverflow.com/questions/11789551/sum-of-total-in-java#comment15664080_11789551)和[這裏](http://stackoverflow.com/questions/11790158/get- SumTotal公司功能於Java的來自MySQL的數據庫#comment15663822_11790158)。整個班級似乎都活躍在這裏? – home 2012-08-04 06:03:31

+1

它可能是一個任務? – MadProgrammer 2012-08-04 06:17:11

回答

0

你應該嘗試以下的事情在你的代碼

DateFormat format = new SimpleDateFormat("yyyy-mm-dd"); 
Date date = format.parse("1343993311"); 

這裏date對象將有java.util.Date對象引用。意味着時間戳被轉換成Date對象。

+0

我必須在int count = 0之後添加上面的行。那時我得到以下異常:線程「main」中的異常java.text.ParseException:Unparseable date:「1343455049」 \t at java.text.DateFormat.parse(DateFormat .java:337) \t at com.xcart.TodayQ.data(TodayQ.java:16) \t at com.xcart.Demo.main(Demo.java:8) – user1570318 2012-08-04 06:25:38