Java日历算术问题

发布于 2024-10-03 13:04:18 字数 859 浏览 2 评论 0原文

无法为我在这里的情况找到解决方案。抱歉,如果之前已经回答过这个问题,但我找不到适合我的答案。

问题:尝试测试用户会话的活动时间是否超过超时限制。

我试图使用会话开始时间和当前系统时间并将它们相减,如果结果大于超时变量 - 终止会话。

所以:

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

时光礼记 2024-10-10 13:04:18

阅读 API,在 calendar.add 方法中,它指出您还需要具体说明要添加的字段。
示例

calendar.add(Calendar.HOUR, 24);

但是您有点不需要使用日历操作。

只需减去 currentTimeInMillis 与以毫秒为单位的开始时间,如果更大(以毫秒为单位)则超时,那么您就得到了答案

long refTime = System.currentTimeMillis();
long now = System.currentTimeMillis(); 
if ( now - refTime > TIMEOUT ) { 
     ... } 

Read the API, in the calendar.add method its stated you need to specific to what field you want to add too.
Example

calendar.add(Calendar.HOUR, 24);

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

long refTime = System.currentTimeMillis();
long now = System.currentTimeMillis(); 
if ( now - refTime > TIMEOUT ) { 
     ... } 
笑咖 2024-10-10 13:04:18

您的示例似乎有一些错误,但这是我的解决方法。

Calendar sessionEndTime = logSession.getStartTime();
sessionEndTime.add(Calendar.MINUTE, TIMEOUT_MINS);
Calendar currentSysTime = Calendar.getInstance();
if (currentSysTime.after(sessionEndTime)) {
//do something
}

Your sample seems to have a few errors in it but here is how I would go about it.

Calendar sessionEndTime = logSession.getStartTime();
sessionEndTime.add(Calendar.MINUTE, TIMEOUT_MINS);
Calendar currentSysTime = Calendar.getInstance();
if (currentSysTime.after(sessionEndTime)) {
//do something
}
放肆 2024-10-10 13:04:18
 for (LogSession logSession : sessionLogList) {

                Calendar sessionStartTime = logSession.getStartTime();
                Long now = System.currentTimeMillis();

                if (now - sessionStartTime.getTimeInMillis() > timeoutLimit) {
                       // Do something
                }
}
 for (LogSession logSession : sessionLogList) {

                Calendar sessionStartTime = logSession.getStartTime();
                Long now = System.currentTimeMillis();

                if (now - sessionStartTime.getTimeInMillis() > timeoutLimit) {
                       // Do something
                }
}
不知所踪 2024-10-10 13:04:18

如果您比较会话开始时间和当前时间,结果将是每个会话都有一个最大年龄,并且无论活动如何都会重置。

您需要 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 simple long fields and assigned the System.currentTimeMilis() you dont need Calendar arithmetic.

裸钻 2024-10-10 13:04:18

我假设您位于某个 Web 容器中并且有一个可用的 web.xml。不能用标准的吗?

<session-config>
 <session-timeout>30</session-timeout> 
</session-config>

I am assuming you are in some web container and have a web.xml available. Could you not use standard?

<session-config>
 <session-timeout>30</session-timeout> 
</session-config>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文