首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Java调度API特定的时间间隔

Java调度API特定的时间间隔
EN

Stack Overflow用户
提问于 2018-03-16 04:06:33
回答 1查看 124关注 0票数 0

我对Java非常陌生,我正在尝试生成一个在特定时间运行的任务。有3种类型的时间表: a)在一年中每给定天数执行一次,b)在一个月内每给定次数执行一次(在特定月份中从1次到10次),c)在一个月中每一周执行(一个月中从1周到5次)。

从a到c的字母是时间表的选项。

我尝试了几种方法,但到目前为止都不起作用。我使用了TimerTask、日历和计时器API。我的最新努力如下:

代码语言:javascript
复制
public class Runner {
public static void runTask(int year, int month, int day, int hour, int minute, int second, String choice) {

    Calendar startTime = Calendar.getInstance();
    startTime.set(Calendar.YEAR, year );
    startTime.set(Calendar.MONTH,month);
    startTime.set(Calendar.DAY_OF_MONTH,day);
    startTime.set(Calendar.HOUR_OF_DAY, hour);
    startTime.set(Calendar.MINUTE, minute);
    startTime.set(Calendar.SECOND, second);
    startTime.set(Calendar.MILLISECOND, 0);

    Calendar bStartTime = Calendar.getInstance();
    bStartTime.set(Calendar.YEAR,year);
    bStartTime.set(Calendar.MONTH,month-1);
    bStartTime.set(Calendar.DAY_OF_MONTH,1);
    bStartTime.set(Calendar.HOUR_OF_DAY,12);
    bStartTime.set(Calendar.MINUTE, 0);
    bStartTime.set(Calendar.SECOND, 0);
    bStartTime.set(Calendar.MILLISECOND, 0);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMM dd");
    long timeToEnd = 0;

    if (choice.equals("a")) {
        Timer time = new Timer();

        time.schedule(new SpecificTask(), startTime.getTime(), TimeUnit.DAYS.toMillis(1));

    }
    else if(choice.equals("b")){
        System.out.println("How many times the task should be done in "+year+"."+month);
        int counter = 0;
        Scanner sc = new Scanner(System.in);
        counter = sc.nextInt();

        Timer time = new Timer();
        time.schedule(new SpecificTask(), bStartTime.getTime(), TimeUnit.DAYS.toMillis(30/counter));
        System.out.println("Date of start : " + sdf.format(bStartTime.getTime()));

    }
    else if(choice.equals("c")){
        Timer time = new Timer();
        time.schedule(new SpecificTask(), startTime.getTime(), TimeUnit.SECONDS.toMillis(2));
    }
    else{
        System.out.println("An error has been occured");

    }

}
public static void main(String[] args) {
    int year,month,day,hour,minute,second;
    String choiceOfSchedule;
    System.out.println("Welcome in The Ultimate Task Scheduler");
    System.out.print("Select a type of schedule: ");
    Scanner sc = new Scanner(System.in);
    choiceOfSchedule = sc.nextLine();
    System.out.println("Now set the task's starting time: ... ");
    System.out.println(" Set year: ");
    year = sc.nextInt();
    System.out.print(" Set month: ");
    month =sc.nextInt();
    System.out.print(" Set day: ");
    day = sc.nextInt();
    System.out.print(" Set hour: ");
    hour = sc.nextInt();
    System.out.print( " Set minutes: ");
    minute = sc.nextInt();
    System.out.print(" Set seconds: ");
    second = sc.nextInt();
    runTask(year,month,day,hour,minute,second,choiceOfSchedule);
EN

回答 1

Stack Overflow用户

发布于 2018-09-14 04:34:53

为每个场景编写一个调度类

在我看来,编写一种方法来处理这种不同的场景是毫无意义的。我会为每个场景编写一个类。在设置调度时,每种任务都知道要使用哪个调度程序类。

代码语言:javascript
复制
public class DaysPerYearScheduler { … }

public class TimesPerMonthScheduler { … }

public class OrdinalWeekPerMonthScheduler { … }

作为一个类比,你正试图在BigDecimal上做一个等价的方法来做加法和减法,乘法和除法,以及平均。这不是很尴尬吗?

学习使用替代Timer类的Executor框架。这已经在Stack Overflow上被多次报道过了。

切勿使用Calendar。这个可怕的类在几年前就被java.time类取代了。避免在java.time.*包之外的任何日期-时间类。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49308309

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档