抽了点时间给你写了,但没时间测。
每隔有分钟输出一次,你自己可以改。
public class Greeting extends HttpServlet {
public void init() throws ServletException {
new Thread(new GreetingThread(), "==>GreetingThread<==").start();
}
public class GreetingThread implements Runnable {
public void run() {
while (true) {
try {
doWork();
Thread.sleep(60000);
} catch (InterruptedException ex) {
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void doWork() {
Date zero = null;
Date eight = null;
Date sixteen = null;
Date twentyFour = null;
Date now = null;
Timestamp nowTime = new Timestamp(System.currentTimeMillis());
DateFormat allTimeFormat = new SimpleDateFormat("HH:mm:ss.SSS");
//Date allTime = null;
try {
zero = allTimeFormat.parse("00:00:00.000");
eight = allTimeFormat.parse("08:00:00.000");
sixteen = allTimeFormat.parse("16:00:00.000");
twentyFour = allTimeFormat.parse("24:00:00.000");
now = allTimeFormat.parse(allTimeFormat.format(nowTime));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (now.after(zero) && now.before(eight)) {
System.out.println("早");
} else if (now.before(sixteen)) {
System.out.println("中");
} else if (now.before(sixteen)) {
System.out.println("下");
} else {
System.out.println("晚");
}
}
}
}
}