Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Time in Java

Options
  • 23-04-2005 2:43pm
    #1
    Registered Users Posts: 78 ✭✭


    I was wondering how difficult it is to take time into account when writing a Java Program? I dont mean using the actual time of day but the time elapsed from the moment the program was compiled. I want a value to depreciate over time if it is not being used by the program. Does this make sense to anybody? Any advice on this would be great,

    Thanks.


Comments

  • Closed Accounts Posts: 28 smack


    not too sure what you want to do. Is this like a session time out.
    Or just a value you want do decrease over time. Why not compare the time from when the program launches, and see how much time has elapsed over the course of the program. If the time elapses a certain amount, then decrease the value of whatever.


  • Registered Users Posts: 78 ✭✭al.


    This is just a value i want to decrease. Basically i have 36 buttons corresponding to 36 prices. If a button is pressed a lot i want the corresponding price to also go up and if a button is not being pressed a lot i want the corresponding price of that button to decrease. I cannot use counters alone, i.e if button_press > 5 increase price because this does not take into account the length of time between button presses. I know this sounds a bit confusing but if anybody understands please point me in the right direction. Thanks.


  • Registered Users Posts: 139 ✭✭soiaf


    So the basic idea is something like this?
    long startTime = System.currentTimeMillis();
    
    // blah blah blah
    // stuff happening here
    
    long endTime = System.currentTimeMillis();
    System.out.println(endTime - startTime);
    

    Basically you could store as a long the last time a particular button was pressed (you could have a bunch of long's for each button say).
    Bit nasty, but might give you some ideas....


  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    As far as I can tell, he has a group of buttons, and wants to detect the frequency of use, as well as the time elapsed since their previous use (soiaf's method), and use a combination of those to apply weights to the buttons.


  • Closed Accounts Posts: 3 reallytired


    It's a little messy but you could do this. Run a seperate thread which waits until a preset time has passed before performing whatever method. Create a stopTime variable for 5 minutes ahead or whatever then create a loop which just repeats setting a nowTime variable and continues while nowTime < stopTime.
    public void run(){			
    		Time nowTime;
    		Time stopTime;
    		nowTime.setTime(System.currentTimeMillis());
    		stopTime.setTime(System.currentTimeMillis() + 300000);
    		while (nowTime.getTime() < stopTime.getTime()) { 
    			nowTime.setTime(System.currentTimeMillis());
    		}
    		doWhatever();		
    	}
    


  • Advertisement
Advertisement