I have a method that has to take a picture of the screen every 0.5 seconds and saves the image in a place on the HD. But I need him to run between 11:55 a.m. and 4:55 p.m. to 5:00 p.m.
I just got to start the Task and I could not stop it.
My doubt is:
How could I schedule, for the thread to run only within a certain period of time.
public class Main {
private Toolkit a = Toolkit.getDefaultToolkit();
private Dimension screenSize = a.getScreenSize();
private Rectangle screenLimit = new Rectangle(screenSize);
private Robot robot;
private File file;
BufferedImage img;
private final static int TWO_AM = 11;
private final static int ZERO_MINUTES = 28;
private static Date getTomorrowMorning1145AM(){
Date date2am = new java.util.Date();
date2am.setHours(TWO_AM);
date2am.setMinutes(ZERO_MINUTES);
return date2am;
}
public Main() {
String path = "c:\\print\\"+System.getProperty("user.name")+"\\";
try {
robot = new Robot();
file = new File(path);
} catch (AWTException e1) {
e1.printStackTrace();
}
if(!file.exists()){
file.mkdirs();
}
TimerTask tt = new TimerTask() {
@Override
public void run() {
try {
tirarPrint(path+"print_" + new Date().getTime() + ".jpg");
} catch (IOException | AWTException e) {
e.printStackTrace();
}
}
};
Timer t = new Timer();
t.schedule(tt, 0, 500);
}
private void tirarPrint(String caminho) throws AWTException, IOException {
img = robot.createScreenCapture(screenLimit);
ImageIO.write(img, "jpg", new File(caminho));
}
public static void main(String[] args) {
new Main();
}
}