Thursday, December 12, 2013

PC Login Duration in JAVA

This code snippest is about getting the time duration from which the user has been logged in using JAVA.

 code:

package com.oksbwn.systeminteraction;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
public class OsLoginTime {
 public static long getSystemUptime() throws Exception {
  long uptime =0000;
  
      Process uptimeProc = Runtime.getRuntime().exec("net stats srv");
      BufferedReader in = new BufferedReader(new InputStreamReader(uptimeProc.getInputStream()));
      String line;
      while ((line = in.readLine()) != null) {
          if (line.startsWith("Statistics since")) {
              SimpleDateFormat format = new SimpleDateFormat("'Statistics since' MM/dd/yyyy hh:mm:ss a");
              Date boottime = format.parse(line);
              uptime = System.currentTimeMillis() - boottime.getTime();
              break;
         }
  }
  return uptime/36000;//in millisecond
  }
}