Java - Run a method on a specific date

I need to execute a method on a specific date of each year, how can I do this in java?

Thank,

Chris.

0
source share
4 answers

In order of preference:

  • Quartz library (recommended).

  • java.util.Timer. Not as powerful as quartz, but good for simple tasks.

  • EJB Timer Service. It is poorly documented, it requires a full Java EE container, and it actually does nothing that does not have Quartz.

+4
source

Check out Timer Class

Method:

scheduleAtFixedRate(TimerTask task, Date firstTime, long period) 
          Schedules the specified task for repeated fixed-rate execution, beginning at the specified time.

let you do what you want. Just make sure you use the correct date.

API, TimerTask, run(). run() , .

+3

- , Quartz Cron,

+2

.

import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

private String getDateTime() {
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Date date = new Date();
    return dateFormat.format(date);
}

, if , , - .

-3

Source: https://habr.com/ru/post/1677405/


All Articles