问题
I am building a java application.I need to monitor how long the user is staying on a particular page.The timer should start on user entering the page.It should pause on user moving away to another page and resumes when user returns to the "page(panel)".It should work like a stopwatch on a particular panel.
回答1:
There are a bunch of Stopwatch implementations floating around. I know the Guava library as one: http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/base/Stopwatch.html. We use one called Java Simon to measure the time it takes to make it through a REST call: https://code.google.com/p/javasimon/
I don't think there is anything for free that will record the timing of page transitions through a JPanel, so you're going to need to figure out the event model yourself. But the stopwatch logic itself is really simple.
回答2:
If you can live without detecting that the user actually sits in front of the screen, you can simply use System.currentTimeMillis intialized when user enters the page.
Another option is to use the JAMon API, it comes with AOP.
来源:https://stackoverflow.com/questions/21694833/how-to-display-and-compute-time-in-java