标签:getc highlight except 格式 length date 已运行 0.00 sub
uptime命令可以查看系统的运行时间和负载
终端输入uptime 04:03:58 up 10 days, 13:19, 1 user, load average: 0.54, 0.40, 0.20
显然这样查出的数据是不能展示给用户来看的,下面上更友好的显示运行时间的代码
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{ 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; } /** * 根据毫秒数计算时间间隔,格式:几天几小时几分 * * @param timeMillisBetweenTwoDate * @return */ public static String calculateInterval(long timeMillisBetweenTwoDate) { if (timeMillisBetweenTwoDate == 0) { return "0分钟"; } StringBuilder str = new StringBuilder(); long p = Math.abs(timeMillisBetweenTwoDate); long day = p / (24 * 3600000); if (day > 0) { str.append(day).append("天"); } p = p % (24 * 3600000); long hour = p / (3600000); if (hour > 0) { str.append(hour).append("小时"); } p = p % (3600000); long minute = p / (60000); if (minute > 0) { str.append(minute).append("分钟"); } p = p % (60000); long second = p / (1000); if (second > 0) { str.append(second).append("秒"); } return str.toString(); } /** * linux 系统中获取运行时间 * @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 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(calculateInterval(getUptimeSecond(s3) * 1000)); } }
标签:getc highlight except 格式 length date 已运行 0.00 sub
原文地址:https://www.cnblogs.com/geekdc/p/9240180.html