Search code examples
javatimermilliseconds

How do I find the milliseconds of a certain time?


I have a program that I would like to run at 9:00 PM my computer time. I need a way to find the milliseconds of that so that I can make the computer loop until it hits it. Something like this:

while(true){
    if(System.currentTimeMillis() == [insert method to find millis].[get](2100)){
         break;
    }
}
// Do stuff

So I just need a way to get those Millis from 9 PM. Any ideas?


Solution

  • Create a new Calendar object (GregorianCalendar is most probable) and use getTimeInMillis().

    Calendar cal = new GregorianCalendar(2013, 12, 18, 21, 0, 0);
    if(System.currentTimeMillis() == cal.getTimeInMillis()){
    
    }
    

    It should be noted that if you want to do this regularly, you might want to look at a scheduled task instead.