Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: How can i store time in a variable for comparsion?

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    9
    My Mood
    Cold
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default How can i store time in a variable for comparsion?

    I'm not sure how to store time in a variable without it having the value of current run time
    I've never done this before.

    scenario
    ( don't look to much into the scenario as it's the theory of storing time I'm interested in learning. )
    (Id have the same question about timers too)

    I have a thread the thread sleeps for a time period called thread_sleep_time
    I created one more variable called thread_total_sleep_time that is thread_total_sleep_time += thread_sleep_time, every time the thread is sleeps.

    but how can I capture store a time in a variable called previous_time ? must I use keyword final so it never updates ?
    for comparing against the current time.

    Thank you


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How can i store time in a variable for comparsion?

    What kind of value is the "time" you want to work with?
    The one often used on computers is the number of millisecs since some point in time.

    how can I capture store a time in a variable called previous_time
    Use an assignment statement to give the variable a value.

    so it never updates
    Only executing the assignment statement once will keep it from being updated.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    9
    My Mood
    Cold
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How can i store time in a variable for comparsion?

    I'm not sure I'm bit confused to be honest.
    I need to do this sort of thing

    if(thread_current_total_sleep_time == thread_previous_total_sleep_time + 2000000)
    {
    update_previous_and _current() // somthing like this.
    }

    or something similar i will know how much time has passed since a last interval update

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: How can i store time in a variable for comparsion?

    I'm confused at what you're trying to do. Is it more than just the basic, checking system time? i.e.,

          long initialTime = System.currentTimeMillis();
          while (somethingIsTrue) {
     
              // .... do something
     
          } 
     
          long finalTime = System.currentTimeMillis();
     
          long mSecDiff = finalTime - initialTime;

Similar Threads

  1. How to store char variable in an array
    By ashboi in forum Java Theory & Questions
    Replies: 1
    Last Post: November 9th, 2012, 12:48 PM
  2. Replies: 0
    Last Post: October 30th, 2012, 10:06 AM
  3. First time poster looking for help accessing a variable from a separate class.
    By royalcrown28 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 19th, 2012, 11:41 PM
  4. Replies: 5
    Last Post: November 16th, 2011, 11:22 AM
  5. [SOLVED] unable to store, display date and time from db to extjs grid
    By VaniRathna in forum Java Servlet
    Replies: 3
    Last Post: November 16th, 2011, 09:36 AM