Created
June 12, 2018 03:29
-
-
Save Faz95210/84af8fcd53332c98f65ac2d1836ad2a4 to your computer and use it in GitHub Desktop.
[Create Cron in Java] Create a cron in java using java utils #Java #Cron #Utils
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Init an action to be run every sunday at 10 AM. | |
*/ | |
private static void initCron() { | |
final Calendar calendar = Calendar.getInstance(); | |
calendar.set( | |
Calendar.DAY_OF_WEEK, | |
Calendar.SUNDAY | |
); | |
calendar.set(Calendar.HOUR_OF_DAY, 10); | |
calendar.set(Calendar.MINUTE, 0); | |
calendar.set(Calendar.SECOND, 0); | |
calendar.set(Calendar.MILLISECOND, 0); | |
java.util.Timer time = new java.util.Timer(); | |
time.schedule(new CronHelper(), calendar.getTime(), TimeUnit.DAYS.toMillis(7)); | |
} | |
class CronHelper extends TimerTask { | |
public CronHelper() { | |
// Your Constructor | |
} | |
/** | |
* The action to be performed by this timer task. | |
*/ | |
@Override | |
public void run() { | |
try { | |
// Do your thing | |
} catch (Exception ex) { | |
System.out.println("error running thread " + ex.getMessage()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment