Thread: Time in Java
April 25th, 2011, 09:59 AM #1
Time in Java
I'm trying to do a Java applet game for uni, and have came upon a stumbling block. How do I do time? You see, I need a car sprite to appear from the right hand side of the screen every few seconds. However, I have no idea to set it up so that Java goes "after x seconds, do this".
Thanks in advance.
May 14th, 2011, 06:41 PM #2
- Join Date
- Aug 2008
It takes the current time in milliseconds from midnight January 1, 1970 UTC.
Set a variable to gather this time at the beginning of the program to get the initial program start time and another variable to get the current time value. Here's an example:
boolean running = true;
initTime = System.currentTimeMillis();
currTime = System.currentTimeMillis();
elapsedTime = currTime - initTime;
System.out.println("Time since elapse in milliseconds: " + elapsedTime);
System.out.println("Time since elapse in seconds: " + elapsedTime/1000);
catch (Exception e)
edit: sorry for no indentation, i blame the "quick post"
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
By newbie~wan in forum IMO CommunityReplies: 10Last Post: December 10th, 2011, 01:38 PM
By bernie1716 in forum Webmastering and ProgrammingReplies: 0Last Post: March 12th, 2011, 11:18 PM
By LostBok in forum Webmastering and ProgrammingReplies: 1Last Post: December 11th, 2006, 03:38 AM
By SiLVerBuRn in forum Webmastering and ProgrammingReplies: 6Last Post: August 1st, 2004, 02:36 AM
By bdhobbs1 in forum General Tech DiscussionReplies: 0Last Post: April 19th, 2004, 06:00 PM