1

I'm currently trying to create a very basic job scheduler. I'm trying to create an app that will check the current local system time. If the time is 12 am, it will download from a link "http://javadl.sun.com/webapps/download/AutoDL?BundleId=58134" to my desktop. Does anyone has a clue to do this with Java? Currently, I'm only able to check current local system time.

package task;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.io.*;
import java.net.URL;


class MyTask extends TimerTask {
 public static void main(String[] args)  {
 Timer timer = new Timer();
 timer.schedule(new MyTask(), 60 * 1000);

 public void run() {
   Date date = new Date();
   if(date.compareTo(midnight) == 0) {
  //Download code
   URL google = new URL("http://www.google.it");
   ReadableByteChannel rbc = Channels.newChannel(google.openStream());
   FileOutputStream fos = new FileOutputStream("google.html");
   fos.getChannel().transferFrom(rbc, 0, 1 << 24);
   }
 }
}
}
Eugene
  • 263
  • 2
  • 5
  • 16

2 Answers2

1

You can implement for example a Timer that fire every 60 seconds and if the time is 12 AM download the file .

    Timer timer = new Timer();
    timer.schedule(new MyTask(), 60 * 1000);


class MyTask extends TimerTask {
    public void run() {
       Date date = new Date();
       if(date.compareTo(midnight) == 0) {
      //Download code
     }
    }
aleroot
  • 71,077
  • 30
  • 176
  • 213
  • hi, thanks for the prompt reply. I think the main issue for me is the download code, how to I write that code, sorry if you misunderstood my question. – Eugene Jan 01 '12 at 13:58
  • Take a look here : http://stackoverflow.com/questions/921262/how-to-download-and-save-a-file-from-internet-using-java – aleroot Jan 01 '12 at 14:01
  • Sorry, I have some questions, I'm not able to compile due to some errors. Like URL, fileoutputstream and transferfrom() is not functional for me. Is it because of the library? I have imported jdk 1.6, but still returns an error. any clue why? – Eugene Jan 01 '12 at 14:31
  • Import java.io namespace : import java.io.*; For the URL Class import java.net.URL; – aleroot Jan 01 '12 at 14:46
  • Hi aleroot, really sorry to trouble you, I have edited the code, but I still have the errors, could you take a look at it. I've replace my initial codes with the edited one above. – Eugene Jan 01 '12 at 15:02
  • You can not insert a method declaration inside a body of another method .... You have inserted the run() method inside the body of the main ... – aleroot Jan 01 '12 at 15:13
0

Fully working example to get you going:

import java.util.Calendar;

public class BasicScheduler {
    public static void main(String[] args) throws InterruptedException {
        while (true) {
            Calendar rightNow = Calendar.getInstance();
            Integer hour = rightNow.get(Calendar.HOUR_OF_DAY);

            if (hour==12) {
                // Do your thing
                // ...



                // Sleep for a long time, or somehow prevent two downloads this time
                // ...
                Thread.sleep(60*1000+1);
            }
            else {
                Thread.sleep(1000);
            }

        }
    }
}
Christian Neverdal
  • 5,655
  • 6
  • 38
  • 93
  • Thanks for the help chirstian. The main problem that I'm encountering is the code to download from that link. Could you help me with this part. Thanks! – Eugene Jan 01 '12 at 14:00
  • I have a question, which java library did you use for thread? – Eugene Jan 01 '12 at 14:09
  • From what I can see in the other comments you have now solved the downloading part. The Thread in my example was just a plain java.lang.Thread. – Christian Neverdal Jan 01 '12 at 15:49
  • hi, I'm also using your part of the code. Must I import java.lang.Thread.*; ? At the moment the thread.sleep is not working for me and I still need to use it. – Eugene Jan 01 '12 at 16:09
  • I have found the cause of my problem. It's due to public static void main(String[] args) throws InterruptedException {" Right now, i'm using throws IOException...what should I do if i need both? – Eugene Jan 01 '12 at 16:15