java 通过查询Timer类的方法,解决以下问题:
1个回答
2013-08-07
展开全部
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import java.util.Calendar;
import java.util.Date;
public class SwingTimerDemo{
static boolean bStop = false;
void init(){
int delay = 10000; //milliseconds, 10seconds
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Calendar calendar1 = Calendar.getInstance();
calendar1.set(Calendar.DATE, calendar1.getActualMaximum(Calendar.DATE));
int month1 = calendar1.get(Calendar.DAY_OF_MONTH);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(new Date());
int month2 = calendar2.get(Calendar.DAY_OF_MONTH);
int hour2 = calendar2.get(Calendar.HOUR_OF_DAY);
if(month1==month2 && hour2==17){
doSomething();
}
}
};
new Timer(delay, taskPerformer).start();
}
void doSomething(){
System.out.println("Timer .............");
}
public static void main(String argv[])throws Exception{
new SwingTimerDemo().init();
System.out.println("Timer started .............");
while(!bStop){
Thread.sleep(1000);
}
}
}
请参考,完整代码。
import java.awt.event.ActionListener;
import javax.swing.Timer;
import java.util.Calendar;
import java.util.Date;
public class SwingTimerDemo{
static boolean bStop = false;
void init(){
int delay = 10000; //milliseconds, 10seconds
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Calendar calendar1 = Calendar.getInstance();
calendar1.set(Calendar.DATE, calendar1.getActualMaximum(Calendar.DATE));
int month1 = calendar1.get(Calendar.DAY_OF_MONTH);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(new Date());
int month2 = calendar2.get(Calendar.DAY_OF_MONTH);
int hour2 = calendar2.get(Calendar.HOUR_OF_DAY);
if(month1==month2 && hour2==17){
doSomething();
}
}
};
new Timer(delay, taskPerformer).start();
}
void doSomething(){
System.out.println("Timer .............");
}
public static void main(String argv[])throws Exception{
new SwingTimerDemo().init();
System.out.println("Timer started .............");
while(!bStop){
Thread.sleep(1000);
}
}
}
请参考,完整代码。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询