Check to run only once a day and when Internet is connected

advertisements

I have a java code that must save an image from a URL, once a day. I want to put the executable jar file in windows startup folder to run every time the windows starts and when connects to internet; but, the windows may be start more than one time every day. So, I want my code checks if has been ran and saved the image today, it don’t run again (the name of saved image is Wallpaper and i don't want to change its name). How can I do this? Thank you.

public static void main(String[] args) throws Exception {
    String imageUrl ="http://imgs.yooz.ir/fc/m/medium-news/0170220/656760513-0.jpg";

    String destinationFile = "E:\\Picture\\Wallpaper.jpg";

    saveImage(imageUrl, destinationFile);
}

public static void saveImage(String imageUrl, String destinationFile) throws IOException {
    URL url = new URL(imageUrl);

    byte[] b = new byte[2048];
    int length;

    try {
        InputStream is=url.openStream();
            OutputStream os = new FileOutputStream(destinationFile);
            while ((length = is.read(b)) != -1) {
                os.write(b, 0, length);
            }
            is.close();
            os.close();
        }
    }catch (UnknownHostException e){
        e.printStackTrace();
    }
}


You could download the image only if the current time is greater than 24 hours after the last modification time of the destination file.