I’m still not sure how System.currentTimeMillis() work, but I want to use it for a game server to handle Banned accounts.
For example, in data base I will have a “bannTime” which will equal (System.currentTimeMillis() + How_much_time_to_ban_in_ms ) in the user’s data.
When the user will login, it will always check if it’s OK using this:
if(bannTime==-1)return;
if(System.currentTimeMillis()>bannTime){
// It's ok you can long in
removeBanFromDataBase();
}else{
// You can not login, you have to wait: (bannTime - System.currentTimeMillis())
return;
}
What I need to know is:
Is it safe to use System.currentTimeMillis() like this as long as the code will always run on one machine ? Even if I reboot the machine, System.currentTimeMillis() will keep incrementing and never go back or start from zero ? Never ?
And what If I change the local time and date on the machine, System.currentTimeMillis() will change too ?
When using
System.currentTimeMillis()you are getting the time of the System, in other words the time of the machine you are running your code on. So when you restart your machine, you will still get its time that will increment even when the machine is turned off for sure.But when you change the time of the machine,
System.currentTimeMillis()will get you the new time, the time you changed. So when the time is changed you will get wrong time.I used this in many classes without holding the matter of changing the clock. You can just use it with only this risk. But I find it a very good choice to work on.