2014-06-16 57 views
-5

我要執行像如何解析Date formate?

該代碼會拋出異常後登錄系統

 Process uptimeProc = Runtime.getRuntime().exec("net stats srv"); 
     BufferedReader in = new BufferedReader(new inputStreamReader(uptimeProc.getInputStream())); 

     String line; 
     while ((line = in.readLine()) != null) 
     { 
       if(line.contains("Statistics since")) 
       { 
        SimpleDateFormat format = new SimpleDateFormat("'Statistics since' MM/dd/yyyy hh:mm:ss a"); 
        //System.out.println("in ijj "+in.readLine()); 
        Date boottime = format.parse(line); 
        System.out.println("Uptime="+uptime+""+boottime); 

        uptime = System.currentTimeMillis() - boottime.getTime(); 


        break; 

       } 
     } 

的&註銷時間

Exception in thread "main" java.text.ParseException: Unparseable date: "Statistics since 6/16/2014 10:08 AM" 
    at java.text.DateFormat.parse(DateFormat.java:337) 
    at programs.GetLoginTimeYaar.getSystemUptime(GetLoginTimeYaar.java:44) 
    at programs.GetLoginTimeYaar.main(GetLoginTimeYaar.java:14) 

PLZ回覆

+0

花費一些時間在閱讀[相關問題和解答](http://stackoverflow.com/search?q=ParseException [java]) –

回答

0

嘗試

new SimpleDateFormat("'Statistics since' MM/dd/yyyy hh:mm a"); 

因爲您的日期不包含秒。

0

您必須刪除在你的模式秒:

new SimpleDateFormat("'Statistics since' MM/dd/yyyy hh:mm a"); 
+0

我還建議更改'MM'到'M'以匹配格式的價值。你給出的格式分析值,但會以不同的格式。 –

+0

感謝它的工作。 – Ramesh