Java日历算术问题
无法为我在这里的情况找到解决方案。抱歉,如果之前已经回答过这个问题,但我找不到适合我的答案。
问题:尝试测试用户会话的活动时间是否超过超时限制。
我试图使用会话开始时间和当前系统时间并将它们相减,如果结果大于超时变量 - 终止会话。
所以:
for (LogSession logSession : sessionLogList) {
Calendar sessionStartTime = logSession.getStartTime();
Calendar currentSysTime = Calendar.getInstance();
currentSysTime.setTime(currentSysTime.getTime());
Calendar activeTime = Calendar.getInstance();
activeTime.setTime(sessionStartTime.getTime());
activeTime.add(activeTime.getTime(), - currentSysTime.getTime());
if ( activeTime.getTime() > timeoutLimit) {
// Do something
}
}
我显然在 activeTime.add 行上收到错误消息,但不确定此处所需的方法。任何帮助将非常感谢。
超时当前设置为 15 分钟,但它是一个可以更改的全局变量。
Having trouble finding a solution for my situation here. Sorry if this has been answered before but I couldn't find one that worked for me.
Problem: Trying to test if a user session has been active for longer than the timeout limit.
I am trying to use the session start time and the current system time and subtract them and if the result is greater than the timeout var - kill the session.
So:
for (LogSession logSession : sessionLogList) {
Calendar sessionStartTime = logSession.getStartTime();
Calendar currentSysTime = Calendar.getInstance();
currentSysTime.setTime(currentSysTime.getTime());
Calendar activeTime = Calendar.getInstance();
activeTime.setTime(sessionStartTime.getTime());
activeTime.add(activeTime.getTime(), - currentSysTime.getTime());
if ( activeTime.getTime() > timeoutLimit) {
// Do something
}
}
I am obviously getting an error message on the activeTime.add line but am unsure of the needed approach here. Any help would be great thanks.
Timeout is currently set to 15 minutes but is a global variable that can be changed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
阅读 API,在 calendar.add 方法中,它指出您还需要具体说明要添加的字段。
示例
但是您有点不需要使用日历操作。
只需减去 currentTimeInMillis 与以毫秒为单位的开始时间,如果更大(以毫秒为单位)则超时,那么您就得到了答案
Read the API, in the calendar.add method its stated you need to specific to what field you want to add too.
Example
But you kinda dont need to use calendar manipulation.
Just make a substraction of curentTimeInMillis versus starting time in millis and if bigger (in millis) then your timeout then you have your answer
您的示例似乎有一些错误,但这是我的解决方法。
Your sample seems to have a few errors in it but here is how I would go about it.
如果您比较会话开始时间和当前时间,结果将是每个会话都有一个最大年龄,并且无论活动如何都会重置。
您需要
logSession
对象中的上次活动时间的属性,并使用它与当前时间进行比较。如果这些时间是简单的long
字段并分配了System.currentTimeMilis()
,则不需要Calendar
算术。If you compare the session start time and the current time, the result will be that every session has a max age, and will reset rgardless of activity.
You need an attribute for the last activitiy time in your
logSession
object and use that to compare to the current time. If these times are simplelong
fields and assigned theSystem.currentTimeMilis()
you dont needCalendar
arithmetic.我假设您位于某个 Web 容器中并且有一个可用的 web.xml。不能用标准的吗?
I am assuming you are in some web container and have a web.xml available. Could you not use standard?