当前位置: 代码迷 >> J2SE >> 怎么获取开机时刻到现在时刻的秒数
  详细解决方案

怎么获取开机时刻到现在时刻的秒数

热度:43   发布时间:2016-04-24 00:24:02.0
如何获取开机时刻到现在时刻的秒数?
如何获取开机时刻到现在时刻的秒数?

------解决方案--------------------
Java code
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;    }}
------解决方案--------------------
String startdate = startstr.substring(7,startstr.length());

--------------

不同的系统可能显示的不一样
  相关解决方案