Java gets Linux uptime and friendly display

Source: Internet
Author: User

First, uptime command

Uptime command to view system uptime and load

Terminal input uptime04:03:58 up ten days, 13:19, 1 user, load average:0.54, 0.40, 0.20
    • Current Time 04:03:58
    • The system has been running for ten days, 13:19
    • Current users online 1 user
    • Average load: 0.54, 0.40, 0.20, last 1 minutes, 5 minutes, 15 minutes system load

Obviously the data is not displayed to the user to see, the more friendly display of the running time code

Second, Uptimeutil
Package Com.starfast.web.util;import java.io.inputstreamreader;import java.io.linenumberreader;/** * get device run time * * @     Author Duchong * @since 2018-06-28 14:28 **/public class Uptimeutil {/** * get results of Linux command execution, cat-like * @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;         /** * Returns the elapsed time in seconds * @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")) {//Intercept to day String day=strarr[0].substring (Strarr[0].indexof ("U                    P ") +2,strarr[0].indexof (" Days ")). Trim ();                    Number of seconds Days=integer.parseint (day) * 24 * 3600;                Number of seconds 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; }/** * Gets the number of seconds in the intermediate field * @param str * @return */public static string Gethms (String str) {string R        Esult=null;        Integer hms=0; if (str.cOntains (":")) {String [] hmsarr=str.split ("\ \:");            int length=hmsarr.length;                    Switch (length) {///Only SEC case 1:hms+=integer.parseint (hmsarr[0]);                Break                    Time Division Case 2:hms+= (Integer.parseint (hmsarr[0]) * 3600 + integer.parseint (hmsarr[1]) *60);                Break Time Division second case 3:hms+= (Integer.parseint (hmsarr[0]) * 3600 + integer.parseint (hmsarr[1]) *60 +integer.parseint (H                    MSARR[2]));            Break        }} else{//Does not contain: can only be divided Hms+=integer.parseint (str) * 60;        } if (hms>0) {result=hms.tostring ();    } return result; }/** * Calculates the time interval based on the number of milliseconds, format: Several hours a day * * @param timemillisbetweentwodate * @return */public static Strin        G Calculateinterval (Long timemillisbetweentwodate) {if (timemillisbetweentwodate = = 0) {return "0 minutes";} StringBuilder str = new StringBuilder ();        Long p = math.abs (timemillisbetweentwodate);        Long day = p/(24 * 3600000);        if (Day > 0) {str.append. Append ("Days");        p = p% (24 * 3600000);        Long hour = p/(3600000);        if (Hour > 0) {str.append (hour). Append ("hours");        } p = p% (3600000);        Long minute = P/(60000);        if (Minute > 0) {str.append (minute). Append ("minutes");        } p = p% (60000);        Long second = P/(1000);        if (Second > 0) {str.append (second). Append ("seconds");    } return str.tostring (); /** * Linux system gets run time * @return */public static String Getrouteruptime () {return Getuptimeseco    nd (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, 3 users, Load average:0.06, 0.09, 0.11";    System.out.println (Calculateinterval (Getuptimesecond (S3) * 1000)); }}

Java gets Linux uptime and friendly display

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.