java擷取開機時間,測試成功!

來源:互聯網
上載者:User

擷取cmd命令,精確到分

 

  1. package lihan;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. /**
  10.  * 取tomcat次已耗用時間
  11.  * @author 李晗
  12.  *
  13.  */
  14. public class readRunTime {
  15.     /**
  16.      * 主函數 (use testing)
  17.      * @param args
  18.      * @throws IOException
  19.      */
  20.     public static void main(String[] args) throws IOException { 
  21.         try {
  22.             String str=readSystemStartTime();
  23.             System.out.print(str);
  24.         } catch (InterruptedException e) {
  25.             // TODO 自動產生 catch 塊
  26.             e.printStackTrace();
  27.         }
  28.               
  29.     }
  30.     
  31. public static String readSystemStartTime() throws IOException,
  32.     InterruptedException {
  33. Process process = Runtime.getRuntime().exec(
  34.         "cmd /c net statistics workstation");
  35. String startUpTime = "";
  36. BufferedReader bufferedReader = new BufferedReader(
  37.         new InputStreamReader(process.getInputStream()));
  38. int i = 0;
  39. String timeWith = "";
  40. while ((timeWith = bufferedReader.readLine()) != null) {
  41.     if (i == 3) {
  42.         System.out.println(timeWith);
  43.         //startUpTime = timeWith.substring(x,y);
  44.         startUpTime = timeWith;
  45.     }
  46.     i++;
  47. }
  48. process.waitFor();
  49. return startUpTime;
  50. }
  51. }

輸出結果:

 

統計資料開始於 2008/11/27 下午 08:03
統計資料開始於 2008/11/27 下午 08:03

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.