Search code examples
javascheduled-tasksschedulingreminders

Schedule launching a method in Java at multiple times


I'm working on a program to upload files to the server at specific times given by the administrator, the administrator enters multiples values (hours, minutes).

Example:

[Hours,Minutes]=  [2,12] [ 2,15],[ 5,20 ] 

I save those values in a CSV file.

  BufferedReader reader;
        try {
            reader = new BufferedReader(new FileReader("C:/Users/BACKENDPC1/Desktop/timer.csv"));


    String line = null;
    while ((line = reader.readLine()) != null) {
        lines.add(line);


    }} catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();}

        //Get the Date corresponding to 11:01:00 pm today.
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("HH");
         Format formatter = new SimpleDateFormat("m");
         Format sec=new SimpleDateFormat("s");

        /*heur=getList().get(i).substring(0, getList().get(i).indexOf(substr));
        minute=getList().get(i).substring(getList().get(i).indexOf(substr) + substr.length());
        System.out.println("Time selected is: "+heur+","+minute);*/
        while (i<lines.size()) {
            heur=lines.get(i).substring(0, lines.get(i).indexOf(substr));
            minute=lines.get(i).substring(lines.get(i).indexOf(substr) + substr.length());
            System.out.println(sdf.format(calendar.getTime()));
            System.out.println(Integer.parseInt(formatter.format(new Date())));
            if(Integer.parseInt(sdf.format(calendar.getTime()))==Integer.parseInt(heur)&&(Integer.parseInt(formatter.format(new Date()))==Integer.parseInt(minute))){


        System.out.println(Integer.parseInt(heur)+"H"+ Integer.parseInt(minute));

        calendar.set(Calendar.HOUR_OF_DAY,Integer.parseInt(heur) );
        calendar.set(Calendar.MINUTE, Integer.parseInt(minute));
        calendar.set(Calendar.SECOND, 0);
        Date time = calendar.getTime();

        timer = new Timer();
        timer.schedule(new RemindTask(), time);
        i++;
            }}
        i=1;

        start();


        /*  timer = new Timer();
        timer.schedule(new RemindTask(), seconds*1000);*/
    }

    class RemindTask extends TimerTask  {

      public void run() {


          up.Uplaod();

        long start = new Date().getTime();
            long end=0; 
            int numIndexed=0;
            boolean cond=true;
            end = new Date().getTime();
            cond=false;

           // System.out.println("Indexing " + numIndexed + " files took "
             // + (end - start) + " milliseconds");

            timer.cancel(); 
            //Terminate the timer thread    

      }

I run this method to schedule the Execution of the upload. It works two times and after that I get an error :

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "Hours"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Reminder.start(Reminder.java:64)
at Reminder.start(Reminder.java:80)
at csvFileUploadMulti$4.actionPerformed(csvFileUploadMulti.java:269)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Can someone help me please?


Solution

  • Dont reinvent the wheel. Try cron.