如何获取开机时刻到现在时刻的秒数?

解决方案 »

  1.   

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;public class Test {
    public static void main(String[] args) throws Exception {
    String startstr = Test.readSystemStartTime();
    String startdate = startstr.substring(7,startstr.length()); Calendar c = Calendar.getInstance();
    c.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(startdate));
    System.out.println("开前到现在的毫秒数:" + (System.currentTimeMillis() - c.getTimeInMillis()));
    } public static String readSystemStartTime() throws IOException, InterruptedException {
    Process process = Runtime.getRuntime().exec("cmd /c net statistics workstation");
    String startUpTime = "";
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));
    int i = 0;
    while ((startUpTime = bufferedReader.readLine()) != null) {
    if (i == 3) {
    break;
    }
    i++;
    }
    process.waitFor();
    return startUpTime;
    }
    }
      

  2.   

    String startdate = startstr.substring(7,startstr.length());--------------不同的系统可能显示的不一样