Search code examples
javadatepollingsystem-tray

Java constantly check date


Is there a way for a Java program which is sitting down there in the system tray to constantly check for the computer date? Maybe not every second, but every minute or so? Could it be done without taking up all of the computer's resources?

At the moment I'm thinking of running a date check method which would just check the date on button press, but that's kinda lame and wouldn't really work like I want it to.


Solution

  • You can use a ScheduledExecutorService - that will only use the resources needed by your checkDate method (+ a slight overhead that won't be noticeable).

    ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
    Runnable r = new Runnable() {
        @Override
        public void run() {
            checkDate();
        }
    };
    scheduler.scheduleAtFixedRate(r, 1, 1, TimeUnit.MINUTES);