我不会写代码,每隔一分钟就会让定时器关闭,我希望时间在偶数间隔内,所以当定时器关闭时第二个值将为零。现在我得到了当前的秒数。从它减去60,所以这是几秒钟直到第一分钟。然后将其乘以1000并将定时器惯性延迟设置为该值。现在,当计时器呼叫nbe关闭时,我得到58秒。我再次运行该程序得到了34,然后再次运行并获得了18
设置代码
Calendar time=Calendar.getInstance();
int seconds=Calendar.SECOND;
seconds=60-seconds; // time until next minut
len=1000*60;
Timer timer = new Timer(len, this);
timer.setInitialDelay(1000*60+seconds*1000);
timer.start();
回拨代码
public void actionPerformed(ActionEvent event)
{
if (event.getID()==0)
{
int p;
double d;
p=t.daySize;
String temp;
Calendar firstTime2=Calendar.getInstance();
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
System.out.println("starting tme");
temp=dateFormat.format(firstTime2.getTime());
System.out.println(temp);
答案 0 :(得分:0)
我不确定你上面提到的Timer类。标准Java Timer类(java.util.Timer)将像这样使用
TimerTask tt = new TimerTask() {
public void run()
{
//your current ActionEvent
}
}
Timer t = new Timer("My Timer");
t.schedule(tt, new Date(wheneverTheFirstOneIs), 1000*60);