参考以下代码:
public class AlarmClock {
private final Scheduler scheduler = new Scheduler();
private final SimpleDateFormat dateFormat =
new SimpleDateFormat("dd MMM yyyy HH:mm:ss.SSS");
private final int hourOfDay, minute, second;
public AlarmClock(int hourOfDay, int minute, int second) {
this.hourOfDay = hourOfDay;
this.minute = minute;
this.second = second;
}
public void start() {
scheduler.schedule(new SchedulerTask() {
public void run() {
soundAlarm();
}
private void soundAlarm() {
System.out.println("Wake up! " +
"It's " + dateFormat.format(new Date()));
// Start a new thread to sound an alarm...
}
}, new DailyIterator(hourOfDay, minute, second));
}
public static void main(String[] args) {
AlarmClock alarmClock = new AlarmClock(7, 0, 0);
alarmClock.start();
}
}
public class DailyIterator implements ScheduleIterator {
private final int hourOfDay, minute, second;
private final Calendar calendar = Calendar.getInstance();
public DailyIterator(int hourOfDay, int minute, int second) {
this(hourOfDay, minute, second, new Date());
}
public DailyIterator(int hourOfDay, int minute, int second, Date date) {
this.hourOfDay = hourOfDay;
this.minute = minute;
this.second = second;
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, second);
calendar.set(Calendar.MILLISECOND, 0);
if (!calendar.getTime().before(date)) {
calendar.add(Calendar.DATE, -1);
}
}
public Date next() {
calendar.add(Calendar.DATE, 1);
return calendar.getTime();
}
}
public interface ScheduleIterator {
public Date next();
}
配置web.xml
com.yohn.timer.ExecTimer
********************************************************************
java源文件
package com.yohn.timmer;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class SetTimerExecute extends TimerTask implements ServletContextListener
{
public void contextDestroyed(ServletContextEvent arg0)
{
System.out.println("服务器停止");
}
public void contextInitialized(ServletContextEvent arg0)
{
System.out.println("服务器启动");
//新建一个时间控件
Timer t = new Timer();
//指定的任务从指定的延迟后开始进行重复的固定延迟执行。
t.schedule(new SetTimerExecute(),1000,1000);
}
public void run()
{
FileWriter fs = null;
BufferedWriter buf = null;
try
{
File f = new File("/test.txt");
if (!f.exists())
f.createNewFile();
fs = new FileWriter(f,true);
buf = new BufferedWriter(fs);
//每天上午7点写入到文件
if(new Date().getHours()==7)
{
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
System.out.print("起床啦!起床啦!");
f.write("定时打印:" + myFormatter.format(new Date();));
buf.newLine();
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
buf.flush();
fs.close();
}
catch (IOException e)
{
}
}
}
}
把打印和写文件放在一个TimerTask子类中
然后通过Timer.scheduledAtFixTime()启动这个task
具体参数请参考API
但是我不太明白你指的“定时器只有延时86400000毫秒(24小时)才能实现,”是什么意思
标签:java,定时器