How to track date change in Java

I have an application that has to perform some actions, each date change says at midnight. Whenever the date changes, the application should be told about it. Any help regarding how to implement the functionality would be appreciated.

+6
java
source share
6 answers

What you are looking for is a planner. Quartz is probably the most used scheduler in the Java world, although Spring has some interesting scheduling features if you already use this framework.

Whichever scheduler you choose, you usually indicate the action (sometimes called the "task") and the time it occurred (the "trigger" in quartz terminology). In your case, you have to start the trigger every day at midnight, and when it is fired, it will do everything you need in accordance with your work.

+7
source share

You can use Quartz as a library and set triggers to run at midnight every night. I think simple triggers should do the job.

Alternatively, you can implement what Quartz does on its own using a separate thread and sleep until the next midnight.

+1
source share

The answer to this question was Quartz . This is the defacto standard for planning in Java. It is fairly easy to use, but it is a bit heavy. If you care about cluster scheduling or JDBC job repositories, quartz may be full.

Fortunately, Spring 3.0 comes with new scheduling features. This allows you to use the simple “do it evey 30 seconds” or “do it every day at midnight” without using quartz. If you're already using Spring, it's pretty easy to set up.

Example:

 <task:scheduler id="scheduler" pool-size="10"/> <task:scheduled-tasks scheduler="scheduler"> <task:scheduled ref="anotherObject" method="anotherMethod" cron="* * 00 * * *"/> </task:scheduled-tasks> 

This will cause the “anotherMethod” method to be called on you bean named “anotherObject” at midnight every day. More information can be found on the Spring website: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/scheduling.html

0
source share

You do not need a large sprawling structure. Something like this should work.

 import java.util.*; public class Midnight { public static void main(String args[]) { MidnightCowboy mc = new MidnightCowboy(); mc.start(); // on with you normal program flow (if any) here } } class MidnightCowboy extends Thread { static final boolean COWS_COME_HOME = false; // Whatever data you need should go here public void run() { while (! COWS_COME_HOME) { GregorianCalendar now = new GregorianCalendar(); long nowMilli = now.getTimeInMillis(); now.add(Calendar.DAY_OF_MONTH, 1); // probably an easier way to set the time to the next midnight now.set(Calendar.HOUR_OF_DAY, 0); now.set(Calendar.MINUTE, 0); now.set(Calendar.SECOND, 0); now.set(Calendar.MILLISECOND, 0); long midnightMilli = now.getTimeInMillis(); long delta = midnightMilli - nowMilli; System.out.println("Waiting " + delta + " milliseconds until midnight."); // How many milliseconds until the next midnight? try { sleep(delta); doSomething(); } catch (InterruptedException e) { System.err.println("I was rudely interrupted!"); } } } void doSomething() { // whatever } } 
0
source share

If you need an easier solution than the whole library, I found a rather simple implementation here . This is basically a Timer class.

When the class is first executed using the Start() method, the code gets the date and time of the current time, then gets the datetime object at midnight and subtracts 2 times to give the time until midnight.

From now on, it sets the timer interval and starts the timer. Now that the interval has been reached and the timer fires its event, I reset the timer using the same process. This will interval for 24. When this timer expires, it will reset and repeat indefinitely.

0
source share

If you do not want to use quartz or any other platform, you can simply use scheduleAtFixedRate . The ScheduledExecutorService method instead (an example of how to instantiate one in a JavaDoc).

The only thing you need to think about is to calculate the start of the first day.

Edit

As Jarnbho noted: This will not stand the switch to daylight saving time and leap seconds. Therefore, keep this in mind when using the Contractor.

-one
source share

All Articles