This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
time since JVM started
Is there a way to get the system start time in Java?
I am trying to make some animations using the Alpha class. Alpha is a Java3D API function that models a time-dependent function into [0,1]. An Alpha instance needs to be told how many milliseconds from the system start to wait before it begins running. It calls this its triggerTime.
I want to be able to write a line of code that starts the Alpha immediately upon execution of the line. It follows that I need to know how much time has passed since the system started. In other words, I need to set alpha.triggerTime to System.currentTimeMillis() - startTime.
So how do I find this startTime? I know that I could call System.currentTimeMillis() as the first line of my code, but this animation is happening in a thread called by a different class than the one that starts the program, and I don't want to have to pass the startTime around to all of these objects. There has to be a cleaner solution.
Thanks,
Jeff
'myAlpha' will start immediately upon execution of following line assuming that 'triggerTime' is '0' (startTime + triggerTime >= currentTime) :
myAlpha.setStartTime(System.currentTimeMillis());
August, InteractiveMesh
Related
Is it possible each time the application starts it can run at a time in the past for demo purposes. i.e all time calls need to return a time in the past!
Run a demo that starts on 1991, I do not want to change the system time. So each time the app starts it sets its time to 1991.
In the project I have a lot of calls of System.currentTimeMillis() but I don't want for every line of code where currentTimeMillis is used to subtract it there I would like maybe overriding System.currentTimeMillis() and internally subtracting the amount of years between now and 1991 and then question is: "Is it possible?".
This question already has answers here:
How do I measure time elapsed in Java? [duplicate]
(15 answers)
Closed 5 years ago.
I'm working on an Android app and I have this problem.
For example, if I delete a file (operation A) and I receive a new file (operation B), I want to know how much time has passed between the two operations. I want it to work also if, in between, the user changes the date of the system, turns off the internet or restarts the device.
I know that exists SystemClock.elapsedRealtime() but its value restarts from zero if the user restarts the device.
I cannot use System.currentTimeMillis() because it changes if user changes the date.
I cannot get a date from the internet.
Thanks
Use System.currentTimeMillis(). It gets the time elapsed since the epoch (January 1st 1970).
You need a global var:
long start;
on the first action:
start = System.currentTimeMillis();
Since it's the time from the epoch, restarting the device isn't going to change it (i.e. System.nanoTime would be reset). However, as with most other methods, it isn't safe from changing the time of the device. If someone changes the time on the device back to the start of the epoch, you will experience some problems.
Note that there is no way to get the exact time since the event happened if the time is changed. I.e. if the user does operation A, waits a few hours, sets the clock back to a few hours ago, there's basically no offline ways you can check that. If you use a server, you can get the time from that, but there's not any way to get the accurate, unmodified time difference offline that's tamper proof (where tampering is changing the time).
TL;DR: System.currentTimeMillis is an offline option, but it isn't safe from time changing. If you need it to show the right time difference independently of the user changing the time of the device, use a server.
EDIT:
If you can't use System.currentTimeMillis or get a time from the internet, you can't measure the time at all. AFAIK, every Java/Android API relies on System.currentTimeMillis (or get the current time some other way). Example: the Date class can be converted to a Long representing the current time in milliseconds. For long-term timing, you either have to use System.currentTimeMillis or a server. System.nanoTime restarts when the JVM restarts. So does elapsedRealTime.
You just need to grab the time from somewhere before and after the activities you want to time and take one from the other. This uses the system clock but you could equally get the real time from some other source
long startTime = System.currentTimeMillis();
// Your code
System.out.println("Operation took " + (System.currentTimeMillis() - startTime) + " milliseconds");
Iam new here, like week ago I started learning to program and Iam working on my text based strategic game in which I would like to make resources gather every x seconds passively.
basicaly my game is based on while (true) loop in which are switch cases and the game keeps waiting for keys to press to gather resources.
I would like the game to gather resources passively every x seconds.
example : every 10 seconds you will receive +1 wood.
I will welcome in help :)
Iam programming in java using NetBeansIDE
Just get the current time in every loop run with this
and check whether the 10 seconds passed since last time you gave +1 wood.
Note the time unit.
I am not sure how you are storing gathered resources, but if you could call a method to look it up based on when the game began. You could store the following variable as part of your game class:
private long startTime = System.currentTimeMillis();
And then get current resources by using the following method:
public int getWood() {
long diffMilliseconds = System.currentTimeMillis() - startTime;
int numSeconds = (int)diffMilliseconds / 1000;
int amtWood = numSeconds/10;
return amtWood + <any other wood gathering/calculating parameters>;
}
This does not account for using your wood. If you want a variable to update every ten seconds, you could use something like the solution here: Print "hello world" every X seconds. This solution uses the TimeTask class. Rather than printing, you could update a amtWood variable.
If there are no restrictions on what classes you can use, try Timer class available as part of Java Swing. You can also look at java.util.Timer class for scheduling your updates.
I have a task that needs to run every 4 years in my application. How can I configure that in cron.xml. I know that <schedule>1 of Jan</schedule> would run it yearly but is there a syntax for running it after X years something like <schedule>every 4 years 1 of Jan</schedule> ?
-Srikanth
The documented schedule format doesn't seem to have support for what you desire.
You could convert such very long intervals in hours and use this format instead (might be tricky to get exact dates/times in some cases - leap years, etc):
every N hours
I just tried a number of hours equivalent to over 4 years, the development server doesn't seem unhappy:
Another possible approach (which also allows precise date/time specs) is to invoke the task yearly as you mentioned, keep track inside your app of the last execution date or the number of invocations and only execute the actual task's job in 1 out of 4 invocations and simply exiting without doing anything in the other 3 invocations.
This question already has an answer here:
Timer in Eclipse for Android Game
(1 answer)
Closed 9 years ago.
I have a 3-level math game. Our professor wants me to put a timer that starts at first level and stops only if the user already finished the 3rd level. I have this 3 activity class since I have 3 levels. I made it like this so that I will not be confused.
My problem is how can I make a timer continuously within each activity? I tried the examples found on the internet but I can able to run it on the first level.
Please help , because I don't have any idea how can I make this possible.
Thankyou :)
You could store the time when the game starts and then just calculate the current gameplay time according to that.
First you need to create the static variable to store the time in, we need it to be static, so it can be shared between classes easily.
public static long time_start = System.currentTimeMillis();
Then if you want to get the current passed time since the game started, you would do the following.
double time = (System.currentTimeMillis() - time_start) / 1000d;
The time variable is the passed time since the game started, and it is in seconds.
Then if you further want to reset the gameplay time at any point you would just call.
time_start = System.currentTimeMillis();
When opening a new Activity, use intent.putExtra(currentTime) so that the next Activity knows where to continue the timer from.