Reputation: 263
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);
}
}
}
}
Upvotes: 1
Views: 302
Reputation: 5375
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);
}
}
}
}
Upvotes: 0
Reputation: 72636
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
}
}
Upvotes: 1