I found this solution here :
strComputer = "." Set objWMIService = GetObject("winmgmts:{(Security)}\\" & _ strComputer & "\root\cimv2") Set colEvents = objWMIService.ExecQuery _ ("SELECT * FROM Win32_NTLogEvent WHERE LogFile = 'Security' AND " & _ "EventCode = 528 AND User = 'fabrikam\\kmyer'") Wscript.Echo colEvents.Count
Just replace the values ββwith the ones you want.
Now this is not Java, but VB code ... However, it seems that it uses the WMI interface that you can use in your Java program. Or you can do something ugly and invoke the batch version of the script from Java (or the scheduled task) and read its output or use the binding.
This, of course, assumes that you want to check it on the user's computer, as your question hinted. If you want to calculate login on a more global level and from different computers, you need to query Active Directory (or another mechanism used by the network infrastructure); knitted thread offers solutions for this.
Update:
You can take a look at the Eric Fitzgerald blog post on Tracking login activity using login events where you have the appropriate codes (as well as complete formulas for accurately tracking time).
Apparently you need event codes 4624 (LOGON) and 4634 (LOGOFF), as well as the others listed there if you plan to use the Fitzgerald formulas to calculate the exact time of activity.
haylem
source share