java获取Linux持续运行时间及友好显示

一、uptime命令

  uptime命令可以查看系统的运行时间和负载

终端输入uptime

04:03:58 up 10 days, 13:19, 1 user, load average: 0.54, 0.40, 0.20
  • 当前时间 04:03:58
  • 系统已运行的时间 10 days, 13:19
  • 当前在线用户 1 user
  • 平均负载:0.54, 0.40, 0.20,最近1分钟、5分钟、15分钟系统的负载

显然这样查出的数据是不能展示给用户来看的,下面上更友好的显示运行时间的代码

二、UptimeUtil

package com.starfast.web.util;

import java.io.InputStreamReader;
import java.io.LineNumberReader;

/**
 * 获取设备运行时间
 *
 * @author DUCHONG
 * @since 2018-06-28 14:28
 **/
public class UptimeUtil {

    /**
     * 获取linux命令执行的结果,cat 之类
     * @param cmd
     * @return
     */
    public static String getCmdResult(String cmd) {

        String result = "";
        try {

            Process process = Runtime.getRuntime().exec(cmd);
            InputStreamReader ir = new InputStreamReader(process.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);
            String line;
            while ((line = input.readLine()) != null){
                result=line;
            }

        }
        catch (java.io.IOException e) {

            System.err.println("IOException " + e.getMessage());

        }
        return result;
    }

    /**
     * 返回运行时间的秒
     * @return
     */
    public static String getUptimeSecond(String str){

        String time=null;
        if(str.contains(",")){
            String [] strArr=str.split(",");

            if(strArr.length>2){
                int hms=0;
                int days=0;

                if(str.contains("days")){

                    //截取到天
                    String day=strArr[0].substring(strArr[0].indexOf("up")+2,strArr[0].indexOf("days")).trim();
                    //天的秒数
                    days=Integer.parseInt(day) * 24 * 3600;
                    //时分秒的秒数
                    hms=Integer.parseInt(getHms(strArr[1].replace("min","").trim()));
                }
                else if(str.contains("day")){
                    //截取到天
                    String day=strArr[0].substring(strArr[0].indexOf("up")+2,strArr[0].indexOf("day")).trim();
                    //天的秒数
                    days=Integer.parseInt(day) * 24 * 3600;
                    //时分秒的秒数
                    hms=Integer.parseInt(getHms(strArr[1].replace("min","").trim()));
                }
                else{

                    String hmsStr=strArr[0].substring(strArr[0].indexOf("up")+2);
                    hms=Integer.parseInt(getHms(hmsStr.replace("min","").trim()));
                }

                Integer totalTime=days+hms;
                time=totalTime.toString();
            }
        }
        return time;
    }

    /**
     * 获取中间字段的秒数
     * @param str
     * @return
     */
    public static String getHms(String str){

        String result=null;
        Integer hms=0;
        if(str.contains(":")){

            String [] hmsArr=str.split("\:");

            int length=hmsArr.length;

            switch (length){
                //只有秒
                case 1:hms+=Integer.parseInt(hmsArr[0]);
                    break;
                //时分
                case 2:hms+=(Integer.parseInt(hmsArr[0]) * 3600 + Integer.parseInt(hmsArr[1]) *60);
                    break;
                //时分秒
                case 3:hms+=(Integer.parseInt(hmsArr[0]) * 3600 + Integer.parseInt(hmsArr[1]) *60 +Integer.parseInt(hmsArr[2]));
                    break;
            }
        }
        else{
            //不包含: 只能是分
            hms+=Integer.parseInt(str) * 60;
        }
        if(hms>0){
            result=hms.toString();
        }
        return result;
    }
    /**
     * 获取运行时间
     * @return
     */
    public static String getRouterUptime(){
        return getUptimeSecond(getCmdResult("uptime"));
    }

    public static void main(String[] args) {

        String s1="14:08:51 up 3 days,  1:04,  2 users,  load average: 0.00, 0.00, 0.00";
        String s5=" 18:09:13 up 1 day,  1:43,  4 users,  load average: 0.51, 0.48, 0.31";
        String s2="14:13:34 up  5:06,  4 users,  load average: 0.00, 0.01, 0.05";
        String s3="16:41:19 up 8 min,  2 users,  load average: 0.56, 0.39, 0.17";
        String s4="18:03:32 up  1:30,  3 users,  load average: 0.06, 0.09, 0.11";

        System.out.println(getUptimeSecond(s5));
    }
}

三、cat  /proc/uptime

/**
     * 返回运行时间的秒-更精确
     * @return
     */
    public static String getUptimeSecond2(String str){
        String result="0";

        if(StringUtils.isNotEmpty(str)) {
            if (str.contains(" ")) {
                String[] re = str.split(" ");

                if (re.length > 0) {
                    String first = re[0];
                    if (first.contains(".")) {
                        result = first.substring(0, first.indexOf("."));
                    }
                    else {
                        result = first;
                    }
                }
            }
            else{
                if (str.contains(".")) {
                    result = str.substring(0, str.indexOf("."));
                }
                else {
                    result = str;
                }
            }
        }
        return result;
    }

/**
     * 获取系统运行时间-更精确
     * @return
     */
    public static String getRouterUptime2(){
        return getUptimeSecond2(getCmdResult("cat /proc/uptime"));
    }

  

原文地址:https://www.cnblogs.com/geekdc/p/9240180.html