开发者

Java Calendar arithmetic issue

开发者 https://www.devze.com 2023-01-26 03:09 出处:网络
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.

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 s开发者_Python百科et to 15 minutes but is a global variable that can be changed.


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 ) { 
     ... } 


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
}


 for (LogSession logSession : sessionLogList) {

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

                if (now - sessionStartTime.getTimeInMillis() > timeoutLimit) {
                       // Do something
                }
}


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.


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消