求一个简单的Java定时器源代码
3个回答
展开全部
我是让它每10毫秒扫描一下。
Calendar ca=null;
Calendar ca1=null;
Timer timer=new Timer();
static Connection con=null;
public void chu(){
ca=Calendar.getInstance();
ca1=(Calendar)ca.clone();
ca1.add(Calendar.SECOND, 20);
}
public void Time(){
float s=ca1.get(Calendar.SECOND)-ca.get(Calendar.SECOND)+(ca1.get(Calendar.MILLISECOND)-ca.get(Calendar.MILLISECOND))/1000f;
if(con==null){
System.out.println("耗时"+String.valueOf(s)+"秒");
if(ca.after(ca1)){
System.out.println("取不到连接");
timer.cancel();
return ;
}
}else{
System.out.println("耗时"+String.valueOf(s)+"秒");
}
ca.add(Calendar.MILLISECOND, 10);
timer.schedule(
new TimerTask() {
public void run() {
System.out.println("时间在溜走。。。。");
Time();
}
}, ca.getTime());
}
void getcon(){
Connection con=null;//获取Connection
chu();
Time();
}
Calendar ca=null;
Calendar ca1=null;
Timer timer=new Timer();
static Connection con=null;
public void chu(){
ca=Calendar.getInstance();
ca1=(Calendar)ca.clone();
ca1.add(Calendar.SECOND, 20);
}
public void Time(){
float s=ca1.get(Calendar.SECOND)-ca.get(Calendar.SECOND)+(ca1.get(Calendar.MILLISECOND)-ca.get(Calendar.MILLISECOND))/1000f;
if(con==null){
System.out.println("耗时"+String.valueOf(s)+"秒");
if(ca.after(ca1)){
System.out.println("取不到连接");
timer.cancel();
return ;
}
}else{
System.out.println("耗时"+String.valueOf(s)+"秒");
}
ca.add(Calendar.MILLISECOND, 10);
timer.schedule(
new TimerTask() {
public void run() {
System.out.println("时间在溜走。。。。");
Time();
}
}, ca.getTime());
}
void getcon(){
Connection con=null;//获取Connection
chu();
Time();
}
展开全部
java API Timer类
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2012-07-09
展开全部
网上搜了一个,修改了一下。。。
public class TimeTool {
static JDialog myFrame=new JDialog();
public static void main(String[] args) {
myFrame.setUndecorated(true);//不显示窗口边框和标题
myFrame.setSize(300, 100);
myFrame.setLayout(null);
final JLabel jLabel=new JLabel("X");//放在右上角做关闭按钮
final JLabel label=new JLabel("该打TM了!"); //提示
jLabel.setFont(new Font("宋体", 0, 14));
label.setFont(new Font("宋体",0,20));
myFrame.getContentPane().setBackground(new Color(255, 255, 255));
JPanel p=((JPanel)myFrame.getContentPane());
p.setBorder(new LineBorder(new java.awt.Color(10,110,10), 1, false));
myFrame.setBounds(Toolkit.getDefaultToolkit().getScreenSize().width-305,
Toolkit.getDefaultToolkit().getScreenSize().height-135, 300, 100);
myFrame.getContentPane().add(jLabel);
myFrame.getContentPane().add(label);
jLabel.setBounds(280, 0, 20, 20);
label.setBounds(100, 20, 100, 50);
label.setVisible(true);
label.setForeground(Color.red);
myFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
jLabel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
myFrame.dispose();
}
public void mouseEntered(MouseEvent e) {
super.mouseEntered(e);
jLabel.setForeground(Color.red);
}
public void mouseExited(MouseEvent e) {
super.mouseExited(e);
jLabel.setForeground(Color.BLACK);
}
});
int i=Timetorun()*60*1000;
int k=10800*1000;
System.out.println("Delay minites ="+Timetorun());
System.out.println("Next delay times ="+"3h");
Timer timer = new Timer();
timer.schedule(new MyTask(), i, k);//在17:30执行此任务,以后每隔3h执行这个MyTask任务.
while(true){
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
if(str.equals("ok")){
timer.cancel();// 终止此计时器,丢弃所有当前已安排的任务。
}
}
}
static class MyTask extends java.util.TimerTask{
public void run() {
Tishikuang();
System.out.println("打卡提醒程序running!");
}
}
public static void Tishikuang(){
myFrame.setVisible(true);
}
public static int Timetorun(){
//获取系统当前时间
Calendar cal=Calendar.getInstance();
int nowhour=cal.get(Calendar.HOUR_OF_DAY);
int nowminite=cal.get(Calendar.MINUTE);
System.out.println(nowhour+"");
System.out.println(nowminite+"");
int dehour=0;
int deminite=0;
//获取延迟时间
if(nowminite<=35 && nowhour<=17){
deminite=35-nowminite;
dehour=17-nowhour;
}
else if(nowminite>35 && nowhour<=17){
deminite=95-nowminite;
dehour=16-nowhour;
}
else if (nowminite<=35 && nowhour>17) {
deminite=35-nowminite;
dehour=41-nowhour;
}
else {
deminite=95-nowminite;
dehour=40-nowhour;
}
//延迟时间为K分钟
int k= dehour*60 + deminite;
return k;
}
}
public class TimeTool {
static JDialog myFrame=new JDialog();
public static void main(String[] args) {
myFrame.setUndecorated(true);//不显示窗口边框和标题
myFrame.setSize(300, 100);
myFrame.setLayout(null);
final JLabel jLabel=new JLabel("X");//放在右上角做关闭按钮
final JLabel label=new JLabel("该打TM了!"); //提示
jLabel.setFont(new Font("宋体", 0, 14));
label.setFont(new Font("宋体",0,20));
myFrame.getContentPane().setBackground(new Color(255, 255, 255));
JPanel p=((JPanel)myFrame.getContentPane());
p.setBorder(new LineBorder(new java.awt.Color(10,110,10), 1, false));
myFrame.setBounds(Toolkit.getDefaultToolkit().getScreenSize().width-305,
Toolkit.getDefaultToolkit().getScreenSize().height-135, 300, 100);
myFrame.getContentPane().add(jLabel);
myFrame.getContentPane().add(label);
jLabel.setBounds(280, 0, 20, 20);
label.setBounds(100, 20, 100, 50);
label.setVisible(true);
label.setForeground(Color.red);
myFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
jLabel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
myFrame.dispose();
}
public void mouseEntered(MouseEvent e) {
super.mouseEntered(e);
jLabel.setForeground(Color.red);
}
public void mouseExited(MouseEvent e) {
super.mouseExited(e);
jLabel.setForeground(Color.BLACK);
}
});
int i=Timetorun()*60*1000;
int k=10800*1000;
System.out.println("Delay minites ="+Timetorun());
System.out.println("Next delay times ="+"3h");
Timer timer = new Timer();
timer.schedule(new MyTask(), i, k);//在17:30执行此任务,以后每隔3h执行这个MyTask任务.
while(true){
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
if(str.equals("ok")){
timer.cancel();// 终止此计时器,丢弃所有当前已安排的任务。
}
}
}
static class MyTask extends java.util.TimerTask{
public void run() {
Tishikuang();
System.out.println("打卡提醒程序running!");
}
}
public static void Tishikuang(){
myFrame.setVisible(true);
}
public static int Timetorun(){
//获取系统当前时间
Calendar cal=Calendar.getInstance();
int nowhour=cal.get(Calendar.HOUR_OF_DAY);
int nowminite=cal.get(Calendar.MINUTE);
System.out.println(nowhour+"");
System.out.println(nowminite+"");
int dehour=0;
int deminite=0;
//获取延迟时间
if(nowminite<=35 && nowhour<=17){
deminite=35-nowminite;
dehour=17-nowhour;
}
else if(nowminite>35 && nowhour<=17){
deminite=95-nowminite;
dehour=16-nowhour;
}
else if (nowminite<=35 && nowhour>17) {
deminite=35-nowminite;
dehour=41-nowhour;
}
else {
deminite=95-nowminite;
dehour=40-nowhour;
}
//延迟时间为K分钟
int k= dehour*60 + deminite;
return k;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询