Guest User

Untitled

a guest
Jun 18th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. public String getMachineUptime() throws IOException {
  2. String[] dic = readData().split(" ");
  3.  
  4. long s = (long) Array.get(dic, 1);
  5. return calculateTime(s);
  6. }
  7.  
  8. private String readData() throws IOException {
  9. byte[] fileBytes;
  10. File myFile = new File("/proc/uptime");
  11. if (myFile.exists()) {
  12. try {
  13. fileBytes = Files.readAllBytes(myFile.toPath());
  14. } catch (java.nio.file.AccessDeniedException e) {
  15. return null;
  16. }
  17.  
  18. if (fileBytes.length > 0) {
  19. return new String(fileBytes);
  20. }
  21. }
  22. return null;
  23. }
  24.  
  25. private String calculateTime(long seconds) {
  26. int day = (int) TimeUnit.SECONDS.toDays(seconds);
  27. long hours = TimeUnit.SECONDS.toHours(seconds)
  28. - TimeUnit.DAYS.toHours(day);
  29. long minute = TimeUnit.SECONDS.toMinutes(seconds)
  30. - TimeUnit.DAYS.toMinutes(day)
  31. - TimeUnit.HOURS.toMinutes(hours);
  32. long second = TimeUnit.SECONDS.toSeconds(seconds)
  33. - TimeUnit.DAYS.toSeconds(day)
  34. - TimeUnit.HOURS.toSeconds(hours)
  35. - TimeUnit.MINUTES.toSeconds(minute);
  36.  
  37. return "Day " + day + " Hour " + hours + " Minute " + minute
  38. + " Seconds " + second;
  39. }
  40.  
  41. long s = (long) Array.get(dic, 1);
  42.  
  43. long s = Long.valueOf((String) Array.get(dic, 1));
  44.  
  45. long s = Long.valueOf(dic[1]);
  46.  
  47. long s = (long) Array.get(dic, 1);
  48.  
  49. long s = Long.parseLong(dic[1]);
Add Comment
Please, Sign In to add comment