public class TimerTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
timer4();
}
private static void timer4() {
// TODO Auto-generated method stub
Calendar calendar=Calendar.getInstance();
calendar.set(Calendar.YEAR, 2018);
calendar.set(Calendar.MONTH, 2);
calendar.set(Calendar.DAY_OF_MONTH, 15);
calendar.set(Calendar.HOUR_OF_DAY, 19);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Date time = calendar.getTime(); // 得出执行任务的时间,此处为今天的19:00:00
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
System.out.println("-------设定要指定任务--------");
try {
copyPicture();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, time, 1000);// 这里设定将延时每天固定执行
}
public static void copyPicture() throws Exception{
File file=new File("D://1.jpg");
File file2=new File("D://"+System.currentTimeMillis()+".jpg");
if(!file2.exists()){
file2.createNewFile();
}
InputStream in=new FileInputStream(file);
OutputStream out=new FileOutputStream(file2);
int len=0;
byte[] buffer=new byte[1024];
while((len=in.read(buffer))!=-1){
out.write(buffer, 0, len);
}
out.close();
in.close();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
timer4();
}
private static void timer4() {
// TODO Auto-generated method stub
Calendar calendar=Calendar.getInstance();
calendar.set(Calendar.YEAR, 2018);
calendar.set(Calendar.MONTH, 2);
calendar.set(Calendar.DAY_OF_MONTH, 15);
calendar.set(Calendar.HOUR_OF_DAY, 19);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Date time = calendar.getTime(); // 得出执行任务的时间,此处为今天的19:00:00
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
System.out.println("-------设定要指定任务--------");
try {
copyPicture();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, time, 1000);// 这里设定将延时每天固定执行
}
public static void copyPicture() throws Exception{
File file=new File("D://1.jpg");
File file2=new File("D://"+System.currentTimeMillis()+".jpg");
if(!file2.exists()){
file2.createNewFile();
}
InputStream in=new FileInputStream(file);
OutputStream out=new FileOutputStream(file2);
int len=0;
byte[] buffer=new byte[1024];
while((len=in.read(buffer))!=-1){
out.write(buffer, 0, len);
}
out.close();
in.close();
}
}