-5

I want login & logout time of system

       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.contains("Statistics since"))
                {
                    SimpleDateFormat format = new SimpleDateFormat("'Statistics since' MM/dd/yyyy hh:mm:ss a");
                    //System.out.println("in  ijj  "+in.readLine());
                    Date boottime = format.parse(line);
                    System.out.println("Uptime="+uptime+""+boottime);

                    uptime = System.currentTimeMillis() - boottime.getTime();


                    break;

               }
        }

after executing this code it throws exception like

Exception in thread "main" java.text.ParseException: Unparseable date: "Statistics since 6/16/2014 10:08 AM"
    at java.text.DateFormat.parse(DateFormat.java:337)
    at programs.GetLoginTimeYaar.getSystemUptime(GetLoginTimeYaar.java:44)
    at programs.GetLoginTimeYaar.main(GetLoginTimeYaar.java:14)

plz reply

SpringLearner
  • 13,738
  • 20
  • 78
  • 116
Ramesh
  • 15
  • 8
  • Spend Some time in Reading [Relevant Questions and Answers](http://stackoverflow.com/search?q=ParseException[java]) – akash Jun 16 '14 at 06:03

2 Answers2

1

You have to remove the seconds in your pattern:

new SimpleDateFormat("'Statistics since' MM/dd/yyyy hh:mm a");
Jens
  • 67,715
  • 15
  • 98
  • 113
  • I'd also recommend changing `MM` to `M` to match the format of the value. The format you've given parses the value, but would format it differently. – Jon Skeet Jun 16 '14 at 06:01
1

Try

new SimpleDateFormat("'Statistics since' MM/dd/yyyy hh:mm a");

since your date doesn't contain seconds.

laune
  • 31,114
  • 3
  • 29
  • 42